I have multiple classes in my project, but for a specific jar I don't want to put all classes into the jar.
<profile>
<id>test</id>
<build>
<finalName>THRechner-test</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<includes>
<include>my.koque.threchner.Einheiten</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Then I run the following command: mvn clean package -Ptest, but the class is not put into the jar. Running the command without the include lines does put all files into the jar.
How can I just include one class into the jar?
You must configure the jar plugin like this :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>**/Einheiten.class</include>
</includes>
</configuration>
</plugin>
Related
I am building my Java application using Maven and the Maven Assembly Plugin to create an executable jar.
As a result, the target folder contains multiple jars and other files. However, I only want to deploy the executable jar file built via the Assembly Plugin.
To do this, I have tried to use mvn deploy:deploy-file and provided it with the following properties:
file
repositoryId
url
artifactId
groupId
version
However, when I execute the command, Maven deploys all files instead of only the executable jar.
I also tried disabling the default build step:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<!-- disable standard deploy -->
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
The build part of my pom.xml looks like this:
<build>
<!--suppress UnresolvedMavenProperty -->
<finalName>${project.artifactId}-${BUILD_DATE}</finalName>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.PAtrackMain</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<!--suppress UnresolvedMavenProperty -->
<Implementation-Build>${BUILD_DATE}</Implementation-Build>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<!-- disable standard deploy -->
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
How can I deploy only the executable jar without the other files?
That is much simpler than you might think.
There are two kinds of artifacts, produced by maven project:
main: ${artifactId}-${version}.${packaging} - this one you would like to not publish
supplemental: everything else produced by plugins (javadoc, sources, assembly, etc)
If project/module packaging is pom, that means following:
project/module may not have main artifact, only supplemental ones
some plugins are not enabled by default (https://maven.apache.org/ref/3.8.6/maven-core/default-bindings.html - compare default bindings for pom and jar packaging)
Thus, all what you need is:
switch module packaging from jar to pom
enable missing plugins: maven-compiler-plugin, maven-resources-plugin, maven-jar-plugin, etc
extra configuration of maven-deploy-plugin is not required
Possible to target only one of the two TestNG xml files listed within my Maven POM File?
My Maven POM file points to two xml files: uat.xml and preprod.xml, how do i target only one of the xml files using maven commands such as mvn clean compile test?
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<!--<executable>C:\Program Files\Java\jdk1.8.0_121\bin\javac.exe</executable> -->
<executable>${env.JAVA_HOME}\bin\javac.exe</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>uat.xml</suiteXmlFile>
<suiteXmlFile>preprod.xml</suiteXmlFile>
</suiteXmlFiles>
<testErrorIgnore>false</testErrorIgnore>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
See below. You will reference the file by name. So,
mvn clean test -Dtestfile=uat.xml
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${testfile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
Could you please advise me on whether or not I can dynamically change Include and Exclude for the Maven Surfire Plugin?
For example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/${param}Spec*.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
I would like to have $param passed from the command line when we perform the Maven test command.
Please give advice if you have a solution. I have tried argline and systemProperties
Thanks!
I don't think there is a way to pass dynamic parameter, but you can use below trick if you have limited combinations of exclusions/inclusions. Trick is using profiles with different combinations like combo1, combo2 etc. Then you can run maven build with specific profile & only those include/exclude will work.
Command = mvn clean package -P combo1
pom.xml
<profiles>
<profile>
<id>combo1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/Combo1Spec*.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>combo2</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/Combo2Spec*.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
When i run my jar file an it comes during runtime to access a class from a dependency (which is included by the maven-addjars-plugin), i get a java.lang.NoClassDefFoundError error.
Note: i also include some jars normaly by just specifying the dependency in the POM.xml, i only use the addjar plugin for the custom jars which i only have locally.
Part of POM.XML (If you need more information, pls tell)
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${project.artifactId}-${project.version}</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Swapper</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.5</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/my-repo</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
....
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen</artifactId>
<version>2.1.4-SNAPSHOT</version>
</dependency>
The swagger lib was copied into "target/com.googlecode.addjars-maven-plugin" folder which idk is the right place to have those libs since i dont see the libraries that are downloaded from maven repository. But the class is not found during runtime.
UPDATE:
When running with mvn exec instead of java -jar the program runs.
Anyone has an idea what i did wrong?
I am using maven-war-plugin in my pom.xml to generate a jar file in parallel with my war file in an java web project build. My maven build is creating war and jar files in the target directory. And only war file is installed to local repository. Is there a way to push the jar file created as well to local repository. Below is the snippet of my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<archiveClasses>true</archiveClasses>
<packagingExcludes>WEB-INF/lib/x*.jar</packagingExcludes>
<webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>
<webResources>
<resource>
<directory>${project.basedir}\src\main\webapp</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
Thanks in advance!
pom.xml content:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>x</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>x</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<archiveClasses>true</archiveClasses>
<packagingExcludes>WEB-INF/lib/x*.jar</packagingExcludes>
<webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>
<webResources>
<resource>
<directory>${project.basedir}\src\main\webapp</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/webapp/WEB-INF/lib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is not the right way to use maven. Maven is all about modularity and as a consequence there is a "one project - one artifact" rule (or recommendation). See also this blog if I can't convince you : How to Create Two JARs from One Project (…and why you shouldn't) . It is about multiple jars but the concept is the same.
I think you should restructure your work into having one separate project for the jar, while the others use it as a dependency.