juniorbansal
juniorbansal

Reputation: 1271

How to add src/test/java contents to the output maven jar that gets created

I see a lot of similar questions. But unable to make this work.

I have tried testresources and build-helper-maven-plugin so far

Also I read in 1 thread how to write my own assembly plugin to do something like that.

But posting this again to see if there are cleaner ways that I don't know of

This is existing code and i got to fix it. The thing is when i open the jar after a successful build i am unable to find the src/test/java classes inside the jar. We got a maven build-helper-maven-plugin and maven-jar-plugin. But I don't see the test classes in it still.

<build>
        <plugins>
          <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>build-helper-maven-plugin</artifactId>
           <version>1.7</version>
           <executions>
             <execution>
               <id>add-source</id>
               <phase>generate-sources</phase>
               <goals>
                 <goal>add-source</goal>
               </goals>
               <configuration>
               <sources>
                    <source>src/test/java</source>
                </sources>
                </configuration>
             </execution>
           </executions>
          </plugin>
          <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <version>2.3.1</version>
           <executions>
             <execution>
               <phase>package</phase>
               <goals>
                 <goal>jar</goal>
               </goals>
               <configuration>
                 <classifier>test</classifier>
               </configuration>
             </execution>
           </executions>
         </plugin>
        </plugins>
    </build>

I see the generated classes in a test-classes directory inside the target folder. But not inside the jar

I want them inside the jar as I am depending on that jar in another project. The other project is not compiling because its importing that test class inside src/test/Java

I cannot create a new project just for this case as I don't have that liberty.

Upvotes: 1

Views: 996

Answers (1)

Matthias J. Sax
Matthias J. Sax

Reputation: 62310

Did you try maven-dependency-plugin (instead of build-helper-maven-plugin) in combination with maven-jar-plugin ?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version><!--$NO-MVN-MAN-VER$-->
    <executions>
        <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>you-project-group-id</groupId>
                        <artifactId>you-project-artifact-id</artifactId>
                        <version>${project.version}</version>
                        <type>test-jar</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 1

Related Questions