Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

maven

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.

like image 546
user320550 Avatar asked Dec 13 '22 21:12

user320550


2 Answers

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>

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");

or more generically:

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

to access the sources

like image 141
Sean Patrick Floyd Avatar answered Dec 28 '22 07:12

Sean Patrick Floyd


The includes property of the jar plugin only allows the inclusion of files from a relative path from the plugin's classesDirectory property. So that won't work unless you copied your .java files to ${project.build.outputDirectory} somehow. You could do something like that with the 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>
like image 28
whaley Avatar answered Dec 28 '22 07:12

whaley