I have a Maven project called my-work
which has two source directories
src/test/java
and src/main/java
. I needed to convert both of them into one JAR file so that other projects could use them, but I ended up converting them into two separate JARs. Now, I need to be able to create source JARs for each of these two JARs.
I first created two JARs from main/
and test/
directories by adding this to my pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
Then, I tried the instructions here to make sources JARs. The problem is that it only creates one source JAR i.e. my-work-0.0.1-SNAPSHOT-sources.jar
. But, I want it to also create my-work-0.0.1-SNAPSHOT-tests-sources.jar
. How do I do that?
Here is the full POM:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>my-work</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!--etc-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<name>mywork</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--Dependencies here-->
</dependencies>
</project>
The source plugin can be used to create a jar file of the project sources from the command line or by binding the goal to the project's build lifecycle. To generate the jar from the command line, use the following command: mvn source:jar.
You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>
To generate both standard source and test source JARs, use the following plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>test-jar</goal>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Note that this will not generate a test JAR (only a standard JAR). To generate both a standard and test JAR, you can use the following plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
I am able to generate sources for test with help of below plugin. can you post your pom file ?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
I would suggest looking at the plugin doco for that goal
https://maven.apache.org/plugins/maven-source-plugin/test-jar-mojo.html
It looks like the test-jar goal uses the default manifest file, which would explain why the sources are combined into the same jar file.
My suggestion is to use the defaultManifestFile setting to point to a new manifest file. You will need to define this file yourself, but then you can ensure that the target jar is given a distinct name, thus making it a different JAR file.
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With