简体   繁体   English

使用IDEA和maven2,如何添加非maven .jar?

[英]Using IDEA with maven2, how to add a non-maven .jar?

I have a .jar that I want included in my IDEA web application project that is using maven2 (pom.xml). 我想在我的IDEA Web应用程序项目中包含一个使用maven2(pom.xml)的.jar。

How can I add a .jar to my project that isn't using maven? 如何在不使用maven的项目中添加.jar?

Easiest thing is to use maven:install-file to install the jar in your local repository, as follows (stolen from the docs : 最简单的方法是使用maven:install-file在本地存储库中安装jar,如下所示(从文档中窃取:

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. 然后像任何其他依赖项一样将它添加到您的POM中。 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). 如果它需要与很多人共享,你可以考虑建立一个像NexusArtifactory这样的本地实例(我们在工作的地方使用Artifactory并且效果很好)。

You basically have three options: 你基本上有三个选择:

  • Install the jar in your local repository using install:install-file (see Guide to installing 3rd party JARs ). 使用install:install-file在本地存储库中install:install-file jar(请参阅安装第三方JAR的指南 )。 This is pretty straightforward and easy but this will make your POM non portable. 这非常简单易行,但这会使您的POM无法移植。 This might be an option if you are alone and doesn't plan to distribute your pom.xml. 如果您独自一人并且不打算分发您的pom.xml,这可能是一个选项。

  • Deploy the jar in a corporate repository using deploy:deploy-file . 使用deploy:deploy-file在公司存储库中deploy:deploy-file jar。 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. 如果您没有使用公司存储库但仍在团队中工作,请设置基于文件的存储库并通过源存储库共享它。

Google addjars maven plugin is a good idea. 谷歌addjars maven插件是个好主意。 This can be done through following steps: 这可以通过以下步骤完成:

  1. Add all dependencies to lib folder codebase. 将所有依赖项添加到lib文件夹代码库。

  2. Add google addjars plugin to <build> 将google addjars插件添加到<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现在将创建一个本地repo并将所有jar作为依赖项添加。 mvn compile will then work as charm. 然后mvn compile将作为魅力。

  4. To let idea resolve dependencies go to module settings -> modules -> dependencies add lib folder. 为了让想法解决依赖关系转到module settings -> modules -> dependencies添加lib文件夹。

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

Trying to guess what the question is: 试着猜出问题是什么:

1) You have a maven project A with jar foo.jar 1)你有一个带有jar foo.jar的maven项目A.
2) You have a non-maven project B that you want to use the same jar in (without using maven) 2)你有一个非maven项目B,你想使用相同的jar(不使用maven)
3) You are on windows? 3)你在窗户上?

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 c:\\documents and settings\\your_username\\.m2\\name_of_jar,抓取jar c:\\documents and settings\\your_username\\.m2\\name_of_jar,并将其粘贴到项目B的WEB-INF / lib目录中

The maven install:install-file is perfect for adding a local JAR to your caches, but if you're using a repository externally (ie Archiva or Nexus), then you're better off making it available to all with "mvn deploy:deploy-file" - similar arguments for that command. maven install:install-file非常适合在缓存中添加本地JAR,但如果您在外部使用存储库(即Archiva或Nexus),那么最好通过“mvn deploy”将其提供给所有人: deploy-file“ - 该命令的类似参数。

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. 只在本地安装意味着如果你出于某种原因清除你的本地缓存(我已经多次这样做以解决本地缓存损坏的这个或那个问题),那么你也松开了本地JAR并且需要记住重新添加它。 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. 如果它通过Nexus或Archiva在本地DSL存储库中,那么你的状态很好 - 只需将生成的groupId,artifactId添加到POM中并继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM