Let's suppose that I have a bunch of code that I need across a lot of projects, but just in tests.
So, I want to create a separated maven project for it, for example, carlos-test-utils
, and ad it as a test dependency in my projects.
But, in my carlos-test-utils
project, I also need JUnit. So, I add it as a test dependency, which obviously doesn't work, because I put my code in src/main/java
.
I would like to hear which is the best way to deal with this kind of thing.
Put deps as provided?
Some dirty tricky thing to copy .java
files across the projects?
Any other thing?
You can manage that via the maven-jar-plugin which should be used for such cases in the following way:
This should be done in the module/project which should provide the test classes and the code should be put in the usual folder for test code src/test/java and not in src/main/java. All the dependencies your Test code needs should be added as usual dependencies with scope test.
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
In the project you like to use the test dependencies just use it like this:
<project>
...
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<type>test-jar</type>
<version>version</version>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With