Question

This must be quite elementary, but I'm fairly new to Maven and I didn't find this info.

I have the typical mavenized Java project (I use Eclipse), with a main artifact (src/main/java -> mylib.jar ) and the additional classes in src/test/java (not only unit tests, but also miscellanous code, samples, etc, that I don't want to include in the main jar).

My question is: how I can be sure that there are no circular dependencies inside the classes-artifacts of my project? Specifically, how can be sure that inadvertently some class of main is depending on some test\? I'm concerned about the possibility of releasing a broken mylib.arj. Eclipse knows about dependency among projects, but here we have only one project. Is there some standard way?

Was it helpful?

Solution

Maven won't actually let you do this. Everything in the src/test directory structure is not on the class path when maven tries to execute the compile goal.

For instance, let's say I accidentally add the following code to my HelloWorld program:

public void badDependency() {
    new HelloWorld_UT();
}

Where HelloWorld_UT is just a standard junit test.

Even though eclipse will report no compile errors in my project, when I try to do a maven compile, I get this error:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.297s
[INFO] Finished at: Sat May 10 23:08:29 CDT 2014
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project scratch: Compilation failure
[ERROR] /C:/eclipse/workspace/Scratch/src/main/java/org/drc/HelloWorld.java:[11,21] C:\eclipse\workspace\Scratch\src\main\java\org\drc\HelloWorld.java:11: cannot find symbol
[ERROR] symbol  : class HelloWorld_UT
[ERROR] location: class org.drc.HelloWorld

OTHER TIPS

Maven will automatically exclude the source code used in test when it builds the final jar. However remember to indicate with < scope>test< /scope>, if your tests have any dependencies that are not used in the main.

For example, if jUnit is used in your tests and you don't want to include jUnit in main, the pom.xml file for the dependencies would look like this:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${dependency.junit.version}</version>
        <scope>test</scope>
    </dependency>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top