Maven和eclipse:将非Maven或外部jar添加到项目中的可靠方法? [英] Maven and eclipse: a reliable way to add non-Maven or external jars to a project?

查看:155
本文介绍了Maven和eclipse:将非Maven或外部jar添加到项目中的可靠方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Maven是伟大的它通常通过在 pom 配置中指定依赖程序包的版本,使我不受jar依赖性的影响,并自动应用它们。它也通过m2e与Eclipse完美结合,使事情在IDE中无缝工作。



这对于Maven全球已知的依赖项是非常好的。然而,有时候,有些图书馆需要包含在Maven资源中不可用的项目中。在这种情况下,我通常将它们添加到我的项目中的 lib / 目录中。只要他们在类路径中,那么事情就会编译。



但是,导入项目时,问题是自动包含它们。我一直忍受这个问题,半封闭的修复和黑客太久了。每当有人安装此项目时,我必须告诉他们手动将 lib / 中的jar添加到Eclipse构建路径中,以便所有错误消失。如下所示:





我正在寻找一种以这种方式自动执行此过程的方式,同时兼顾 mvn 命令行程序和Eclipse:更重视Eclipse,因为在导入它们时只需要编译的项目就很好。



我不想设置一个repo服务器,我也没有任何内部的专有组件,将保证在本地设置任何东西。我只是有一些jar文件,开发人员不使用Maven;我想和他们一起编译...我应该可以把它们包含在我的软件的分发中吧?



我真的寻找一种合理的方式来实现这一点,这也将在Eclipse中工作,而不是大惊小怪。 这是一个解决方案我发现有希望,但是
肯定不是这个问题的权威解决方案。唯一其他的事情是关闭的是 maven-addjars-plugin ,其中工程可以,但只在命令行。这个插件并不错,并且有一个非常合理的配置:

 < plugin> 
< groupId> com.googlecode.addjars-maven-plugin< / groupId>
< artifactId> addjars-maven-plugin< / artifactId>
< version> 1.0.5< / version>
<执行>
< 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>

但是,试图让它在Eclipse中运行需要将以下关于生命周期映射的错误添加到您的 pom.xml ,我从来没有上过工作;我甚至不认为它被配置为实际添加到Eclipse构建路径。

 < pluginManagement> 
< plugins>
<! - 此插件的配置仅用于存储Eclipse m2e设置。它对Maven构建本身没有影响.-->
< plugin>
< groupId> org.eclipse.m2e< / groupId>
< artifactId>生命周期映射< / 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>


解决方案

1)可以使用系统范围依赖关系

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

2)您可以将您的x.jar复制到本地maven仓库作为



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



并添加一个依赖关系作为

 <依赖关系> 
< groupId> test< / groupId>
< artifactId> x< / artifactId>
< version> 1.0< / version>
< / dependency>


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:

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>

解决方案

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>

这篇关于Maven和eclipse:将非Maven或外部jar添加到项目中的可靠方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆