tags:

views:

341

answers:

2

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"&gt;
  <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&lt;/url&gt;

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

+1  A: 

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>
whaley
Hi tried this out. However the jar file packages contains only META-INF folder. The .java and .class files dont get bundled in the jar. Tried with mvn package. Have edited my original post with the updated pom.xml.
remove the <includes> from the jar plugin (or remove the .jar plugin altogether and give it a try). My test I did locally didn't explicitly define the jar plugin in the pom. Let me know the results of that.
whaley
See http://pastie.org/979847 for evidence that it at least worked on my end.
whaley
It works now. I removed the <includes> and it bundles the .java also. I tried to remove the jar plugin but that never created the tests jar for me. So i guess we need to override the jar plugin configuration to add the test-jar goal to it.Thanks for your help.
You could show your thanks by accepting the answer and/or upvoting the answer (and do the same for other folks who have answered your other questions).
whaley
Thats the first thing i tried Whaely. But it says that Vote Up requires 15 reputation. Right now for me it shows 7 only :(
A: 

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

seanizer