简体   繁体   English

Maven 和 Eclipse:向项目添加非 Maven 或外部 jar 的可靠方法?

[英]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.通过在pom配置中指定依赖包的版本,它主要让我远离 jar 依赖地狱,并自动应用它们。 It also has great integration with Eclipse via m2e, so that things work seamlessly in an IDE.它还通过 m2e 与 Eclipse 进行了很好的集成,因此可以在 IDE 中无缝地工作。

This is all great for dependencies that are globally known to Maven.这对于 Maven 全球已知的依赖项非常有用。 However, sometimes, there are libraries that need to be included in a project that is not available in the Maven repos.但是,有时,有些库需要包含在 Maven 存储库中不可用的项目中。 In this case, I usually add them to a lib/ directory in my project.在这种情况下,我通常将它们添加到我的项目中的lib/目录中。 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.每次有人安装这个项目时,我都必须告诉他们手动将lib/的 jar 添加到他们的 Eclipse 构建路径中,以便所有错误消失。 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.我正在寻找一种方法来自动化这个过程,它可以同时与mvn命令行程序和 Eclipse 一起使用:更多地强调 Eclipse,因为当你导入它们时有项目可以编译是很好的。

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.我不想为此设置 repo 服务器,也没有任何内部专有组件可以保证在本地设置任何内容。 I just have some jar files where the developers don't use Maven;我只有一些开发人员不使用 Maven 的 jar 文件; 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.我真的在寻找一种合理的方法来实现这一点,它也可以在 Eclipse 中毫不费力地工作。 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.唯一接近的另一件事是maven-addjars-plugin ,它可以正常工作,但只能在命令行上使用。 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;然而,试图让它在 Eclipse 中运行涉及将以下关于生命周期映射的混乱添加到您的pom.xml ,我从未开始工作; I don't even think it is configured to actually add anything to the Eclipse build path.我什至不认为它被配置为实际向 Eclipse 构建路径添加任何内容。

<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 1)您可以使用系统范围依赖

    <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 2)您可以将您的 x.jar 复制到本地 Maven 存储库中

repository/test/x/1.0/x-1.0.jar存储库/测试/x/1.0/x-1.0.jar

and add a dependency as并添加一个依赖项

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

You can use maven to install files from a project\\lib folder to the local repo with the maven-install-plugin as below.您可以使用 maven 使用 maven-install-plugin 将文件从 project\\lib 文件夹安装到本地存储库,如下所示。 I have done this before with JDBC drivers.我以前用 JDBC 驱动程序做过这个。 You might have to create a separate pom for it and execute it with mvn -f installdeps.pom or something like that.您可能需要为它创建一个单独的 pom 并使用 mvn -f installdeps.pom 或类似的东西来执行它。

If you can get it to play nice and bind with a lifecycle like validate or something, then you can use the m2e plugin with Eclipse and it just might play nice and read dependencies straight from the pom.xml and install the jars as needed to the local repo.如果你可以让它运行良好并绑定像验证或其他东西的生命周期,那么你可以将 m2e 插件与 Eclipse 一起使用,它可能会运行良好并直接从 pom.xml 读取依赖项,并根据需要将 jars 安装到本地回购。

    <plugin>
        <!-- We dont want children attempting to install these jars to the repo. -->
        <inherited>false</inherited>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <executions>
            <execution>
                <id>Microsoft JDBC Driver File 1</id>
                <phase>install</phase>
                <goals>
                    <goal>install-file</goal>
                </goals>
                <configuration>
                    <file>lib/sqljdbc4.jar</file>
                    <groupId>com.microsoft</groupId>
                    <artifactId>microsoft-jdbc-driver</artifactId>
                    <version>4.0</version>
                    <packaging>jar</packaging>
                </configuration>
            </execution>
            <execution>
                <id>ojdbc5</id>
                <phase>install</phase>
                <goals>
                    <goal>install-file</goal>
                </goals>
                <configuration>
                    <file>lib/ojdbc5.jar</file>
                    <groupId>com.oracle</groupId>
                    <artifactId>ojdbc5</artifactId>
                    <version>11.1.2</version>
                    <packaging>jar</packaging>
                </configuration>
            </execution>
        </executions>
    </plugin>

It seems to me that you could use the Maven Resources Plugin to do this for you.在我看来,您可以使用Maven 资源插件为您完成此操作。 Just bind copying of resources to an appropriate lifecycle phase (a phase prior to compilation).只需将资源复制绑定到适当的生命周期阶段(编译之前的阶段)。 You'll most likely need to tweak the maven-compiler-plugin so that these libraries are on the classpath when compiling and at runtime.您很可能需要调整 maven-compiler-plugin,以便这些库在编译时和运行时位于类路径中。

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

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