Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Maven to include the jar dependency, not the subproject source directory in Eclipse?

I use Apache Maven to manage my Java libs (jar) and some of projects that use the libs. For convinience, I use mvn eclipse:eclipse to generate Eclipse project files which can be imported into Eclipse workspace for editing.

Problems arise when I edit the main project and Java lib projects in the same Eclipse workspace. That is, mvn eclipse:eclipse includes src path dependency in .classpath file, not the jar dependency as expected.

Say I have a Jave lib project named mylib. The corresponding jar file mylib.jar has been deployed to a private Maven repo maintained by me. In order to use mylib.jar in the main project, the following dependency is included in pom.xml.

<!-- pom.xml for the main project -->
<dependency>
  <groupId>namespace.my</groupId>
  <artifactId>mylib</artifactId>
  <version>[1.0, )</version>
</dependency>

mvn compile and mvn test work perfect, in which mylib.jar is automatically downloaded from my repo. However, when trying mvn eclipse:eclipse, I find the generated .classpath file doesn't include mylib.jar dependency as expected. Instead, it includes source file directory for mylib as follows.

<!-- .classpath file generated by mvn eclipse:eclipse -->
<classpathentry kind="src" path="/mylib"/>

It seems that Maven reads Eclipse's metadata and finds mylib and the main project coexits in the same workspace. Therefore maven includes the source for my good. Damn. How can I tell maven to ignore the local project source and just include the jar file?

like image 366
Jianwen W. Avatar asked Sep 25 '12 06:09

Jianwen W.


1 Answers

I believe this is actually because they're related projects in Eclipse. So if you right-click on your main project and go to Project References your lib project should be ticked.

If you run mvn eclipse:eclipse it will automatically add project references for any sub-projects (modules). You can change this with the useProjectReferences property of the maven eclipse plugin.

It defaults to true, but

When set to false, the plugin will not create sub-projects and instead reference those sub-projects using the installed package in the local repository

To use the property either set the property in your pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <useProjectReferences>false</useProjectReferences>
            </configuration>
        </plugin>
    <plugins>
<build>

Or as a property on the command line:

mvn eclipse:eclipse -Declipse.useProjectReferences=false
like image 58
James Bassett Avatar answered Oct 19 '22 02:10

James Bassett