添加 java 源代码(.java 文件)以在 Maven 中测试 jar [英] Adding java source (.java files) to test jar in Maven
问题描述
我正在使用我的 pom.xml 并且能够为 src/main/java(比如 app.jar)以及 src/test/java(比如 app-test.jar)生成 jar.我还能够将我的 java 源代码作为 app.jar 的一部分包含在内(即在 jar 中有我的 .class 和我的 .java 文件).
I'm making use of my pom.xml and am was able to generate the jar for src/main/java (say app.jar) as well as for src/test/java (say app-test.jar). I was also able to include my java sources as part of the app.jar (i.e. have both my .class as well as my .java files in the jar).
但是对于我的 app-test.jar,我无法在其中包含我的 .java 文件.
However for my app-test.jar, i'm not able to include my .java files in it.
这是我的 pom.xml:
This is my pom.xml:
<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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
任何帮助将不胜感激.
谢谢.
关于 Whaley 建议的帖子更新:
尝试了 maven-antrun-plugin,但是在运行 mvn 包 之后,我现在在我的 tests.jar 中得到的只是 META-INF 文件夹..java 和 .class 未包含在内:
Tried the maven-antrun-plugin, but rt now after running mvn package all i'm getting inside my tests.jar is the META-INF folder. .java and .class are not getting included:
这是 pom.xml 的一部分
This is the part of the pom.xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-include-sources</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.testOutputDirectory}">
<fileset dir="${project.build.testSourceDirectory}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
谢谢.
推荐答案
我认为您可能需要使用程序集插件生成自定义程序集:
I think you probably need to generate a custom assembly using the assembly plugin:
http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html
但更简洁的解决方案是使用源插件单独打包测试源
But a much cleaner solution would be to package test sources separately using the source plugin
http://maven.apache.org/plugins/maven-source-plugin/test-jar-mojo.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-test-sources</id>
<goals>
<goal>test-jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
然后,您可以在类路径上同时拥有测试源 jar 和测试类 jar,并执行以下操作:
you can then have both the test source jar and the test class jar on your classpath and do something like this:
YourClassName.class.getResource("YourClassName.java");
或更笼统地说:
public static InputStream getSourceForClass(final Class<?> clazz) {
final String baseName = clazz.getSimpleName();
return clazz.getResourceAsStream(baseName + ".java");
}
访问资源
这篇关于添加 java 源代码(.java 文件)以在 Maven 中测试 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!