添加java源代码(.java文件)以测试Maven中的jar

use*_*550 4 maven

我正在使用我的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>
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

谢谢.

有关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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Sea*_*oyd 10

我想你可能需要使用程序集插件生成自定义程序集:

http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html

但更清洁的解决方案是使用源插件单独打包测试源

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>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在类路径上同时拥有测试源jar和测试类jar,并执行以下操作:

YourClassName.class.getResource("YourClassName.java");
Run Code Online (Sandbox Code Playgroud)

或更一般地说:

public static InputStream getSourceForClass(final Class<?> clazz) {
    final String baseName = clazz.getSimpleName();
    return clazz.getResourceAsStream(baseName + ".java");
}
Run Code Online (Sandbox Code Playgroud)

访问来源