【发布时间】:2010-05-26 19:26:30
【问题描述】:
我正在使用我的 pom.xml,并且能够为 src/main/java(比如 app.jar)以及 src/test/java(比如 app-test.jar)生成 jar .我还能够将我的 java 源代码作为 app.jar 的一部分包含在内(即,我的 .class 和 .java 文件都在 jar 中)。
但是对于我的 app-test.jar,我无法在其中包含我的 .java 文件。
这是我的 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 不包括在内:
这是 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>
谢谢。
【问题讨论】:
-
请问您为什么需要这个,因为可能有更好的解决方案。
-
只是我们需要访问这个jar文件并利用反射来首先获取一个类文件。一旦我们掌握了这一点,我们就想读取它的 java 文件并执行一些操作。我相信 maven 也会让我们能够包含测试 jar 的 java 源代码。
标签: maven