Adding java source (.java files) to test jar in Maven

Posted by user320550 on Stack Overflow See other posts from Stack Overflow or by user320550
Published on 2010-05-26T19:26:30Z Indexed on 2010/05/27 13:01 UTC
Read the original article Hit count: 158

Filed under:

Hi all,

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).

However for my app-test.jar, i'm not able to include my .java files in it.

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>

Any help would be appreciated.

Thanks.

Update on post on Whaley's suggestion:

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:

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>

Thanks.

© Stack Overflow or respective owner

Related posts about maven