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

时间:2010-05-26 19:26:30

标签: 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>

任何帮助都将不胜感激。

感谢。

关于Whaley建议的帖子更新:

尝试了maven-antrun-plugin,但是现在运行 mvn package 后,所有我进入我的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>

感谢。

2 个答案:

答案 0 :(得分: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>

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

YourClassName.class.getResource("YourClassName.java");

或更一般地说:

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

访问来源

答案 1 :(得分:3)

jar插件的includes属性只允许包含来自插件的classesDirectory属性的相对路径的文件。除非你以某种方式将.java文件复制到${project.build.outputDirectory},否则这将无效。您可以使用maven-antrun-plugin执行类似的操作:

<build>
        <plugins>
            <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.outputDirectory}">
                                    <fileset dir="${project.build.SourceDirectory}"/>
                                </copy>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>