Maven-如何为测试包创建源jar? [英] Maven - How do I create source jar for test package?

查看:86
本文介绍了Maven-如何为测试包创建源jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为my-work的Maven项目,该项目有两个源目录 src/test/javasrc/main/java.我需要将它们都转换为一个JAR文件,以便其他项目可以使用它们,但是最终我将它们转换为两个独立的JAR.现在,我需要能够为这两个JAR中的每一个创建源JAR.

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.

我首先从main/test/目录创建了两个JAR,方法是将其添加到我的pom.xml:

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>

然后,我尝试了此处的说明制作源JAR.问题在于它仅创建一个源JAR,即my-work-0.0.1-SNAPSHOT-sources.jar.但是,我希望它也创建my-work-0.0.1-SNAPSHOT-tests-sources.jar.我该怎么办?

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?

这是完整的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>

推荐答案

要生成标准源JAR和测试源JAR,请使用以下插件:

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>

请注意,这不会生成测试JAR(仅是标准JAR).要生成标准JAR和测试JAR,可以使用以下插件:

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>

这篇关于Maven-如何为测试包创建源jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆