javaeclipsemavenm2eclipsem2e

Maven and eclipse: a reliable way to add non-Maven or external jars to a project?


Maven is great. It mostly keeps me out of jar dependency hell by specifying versions of dependent packages in the pom configuration, and applies them automatically. It also has great integration with Eclipse via m2e, so that things work seamlessly in an IDE.

This is all great for dependencies that are globally known to Maven. However, sometimes, there are libraries that need to be included in a project that is not available in the Maven repos. In this case, I usually add them to a lib/ directory in my project. As long as they are in the classpath then things compile.

However, the problem is getting them to be included automatically when importing a project. I've been tolerating this problem with half-baked fixes and hacks for far too long. Every time someone installs this project, I have to tell them to manually add the jars in lib/ to their Eclipse build path so that all the errors go away. Something like the following:

enter image description here

I'm searching for a way to automate this process in a way that works with both the mvn command line program and Eclipse: more an emphasis on Eclipse, because it's nice to have projects that just compile when you import them.

I don't want to set up a repo server for this, nor do I have any in-house proprietary components that would warrant setting up anything locally. I just have some jar files where the developers don't use Maven; and I want to compile with them...I should just be able to include them in the distribution of my software, right?

I'm really looking for a reasonable way to implement this that will also work in Eclipse with no fuss. This is one solution I've found promising, but there definitely doesn't seem to be an authoritative solution to this problem. The only other thing that comes close is the maven-addjars-plugin, which works okay but only on the commandline. This plugin is not bad, and has a pretty reasonable configuration:

<plugin>
    <groupId>com.googlecode.addjars-maven-plugin</groupId>
    <artifactId>addjars-maven-plugin</artifactId>
    <version>1.0.5</version>
    <executions>
        <execution>
            <goals>
                <goal>add-jars</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>${project.basedir}/lib/java-aws-mturk</directory>
                    </resource>
                    <resource>
                        <directory>${project.basedir}/lib/not-in-maven</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

However, trying to get it to run in Eclipse involves adding the following mess about lifecycle mapping to your pom.xml, which I have never gotten to work; I don't even think it is configured to actually add anything to the Eclipse build path.

<pluginManagement>
    <plugins>
        <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>
                                    com.googlecode.addjars-maven-plugin
                                </groupId>
                                <artifactId>
                                    addjars-maven-plugin
                                </artifactId>
                                <versionRange>
                                    [1.0.5,)
                                </versionRange>
                                <goals>
                                    <goal>add-jars</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

Solution

  • 1) you can use system scope dependency

        <dependency>
            <groupId>test</groupId>
            <artifactId>x</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/x.jar</systemPath>
        </dependency>
    

    2) you can copy your x.jar to local maven repository as

    repository/test/x/1.0/x-1.0.jar

    and add a dependency as

        <dependency>
            <groupId>test</groupId>
            <artifactId>x</artifactId>
            <version>1.0</version>
        </dependency>