-->

Using IDEA with maven2, how to add a non-maven .ja

2020-07-18 03:43发布

问题:

I have a .jar that I want included in my IDEA web application project that is using maven2 (pom.xml).

How can I add a .jar to my project that isn't using maven?

回答1:

You basically have three options:

  • Install the jar in your local repository using install:install-file (see Guide to installing 3rd party JARs). This is pretty straightforward and easy but this will make your POM non portable. This might be an option if you are alone and doesn't plan to distribute your pom.xml.

  • Deploy the jar in a corporate repository using deploy:deploy-file. This is of course the ideal and preferred scenario in a corporate environment.

  • If you're not using a corporate repository but are still working in team, setup a file based repository and share it through your source repository.



回答2:

Easiest thing is to use maven:install-file to install the jar in your local repository, as follows (stolen from the docs:

mvn install:install-file -Dfile=path-to-non-maven.jar \
                      -DgroupId=some-id \
                      -DartifactId=some-artifact \
                      -Dversion=some-version \
                      -Dpackaging=jar

Then add it to your POM like any other dependency. If it needs to be shared with many people, you may consider setting up a local instance of something like Nexus or Artifactory (we use Artifactory where I work and it works well).



回答3:

Google addjars maven plugin is a good idea. This can be done through following steps:

  1. Add all dependencies to lib folder codebase.

  2. Add google addjars plugin to <build>

    <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>${basedir}/lib</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    
  3. mvn compile will now create a local repo and add all jars as dependencies. mvn compile will then work as charm.

  4. To let idea resolve dependencies go to module settings -> modules -> dependencies add lib folder.

Reference: https://code.google.com/archive/p/addjars-maven-plugin/issues



回答4:

Trying to guess what the question is:

1) You have a maven project A with jar foo.jar
2) You have a non-maven project B that you want to use the same jar in (without using maven)
3) You are on windows?

If this is true, then:

Go grab the jar from c:\documents and settings\your_username\.m2\name_of_jar, and stick it in the WEB-INF/lib directory of project B



回答5:

The maven install:install-file is perfect for adding a local JAR to your caches, but if you're using a repository externally (i.e. Archiva or Nexus), then you're better off making it available to all with "mvn deploy:deploy-file" - similar arguments for that command.

Installing just locally means that if you purge your local cache for whatever reason (i've ended up doing this a number of times to solve this or that problem with corrupted local cache), then you loose the local JAR as well and need to remember to re-add it again. If it's in the local DSL repository through Nexus or Archiva, you're in good shape - just add the resulting groupId, artifactId into the POM and carry on.