Maven - How do I create source jar for test package?

MasterJoe2 :

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>
Justin Albano :

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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=36583&siteId=1