简体   繁体   English

将基于Maven的依赖项的传递依赖项收集到非Maven项目的类路径中

[英]Collecting Maven-based dependency's transitive dependencies to a non-Maven project's classpath

I'm using a library that uses Maven to compile and test. 我正在使用一个使用Maven编译和测试的库。
I was able to compile the library without any problems. 我能够毫无问题地编译库。 While compiling, it seemed as if it downloaded all the dependencies of the library. 在编译时,似乎它下载了库的所有依赖项。

Now, I'm trying to use the library in my project. 现在,我正在尝试在我的项目中使用该库。 When I compiled the library, I found that a folder called target was created in the library folder and inside that folder, there was another folder called classes . 当我编译库时,我发现在库文件夹中创建了一个名为target的文件夹,在该文件夹中,还有另一个名为classes文件夹。 I added the classes folder to my classpath. 我将classes文件夹添加到了我的类路径中。 However, whenever I try to use that library in my project which does not use Maven, it says that it can't find that library's dependencies. 但是,每当我尝试在我的项目中使用不使用Maven的库时,它就说它无法找到该库的依赖项。

How do I add all of that library's dependencies to my classpath? 如何将所有库的依赖项添加到类路径中?
Do I need to go and manually download all the library's dependencies and add them to the classpath? 我是否需要手动下载所有库的依赖项并将它们添加到类路径中?
Is there any way I can have Maven do that for me? 有什么方法可以让Maven为我这样做吗?
What do I need to do so that I can use the library in my project? 我需要做什么才能在项目中使用库?

My project is in a completely separate directory than the library. 我的项目位于与库完全不同的目录中。 Right now, my project seems to be able to load the library files correctly, but just not the library dependencies. 现在,我的项目似乎能够正确加载库文件,但不是库依赖项。

When you executed mvn install for that library, it should have created a jar file and put it at target/libaryname-version.jar . 当您为该库执行mvn install ,它应该创建一个jar文件并将其放在target/libaryname-version.jar It would be better to depend on this final jar instead of the contents of the classes folder. 最好依赖于这个最终的jar而不是classes文件夹的内容。 Maven also has a goal to download all dependencies of a project. Maven还有一个目标,即下载项目的所有依赖项。 You can execute 你可以执行

mvn dependency:copy-dependencies

Inside the libraries folder and it will copy all dependency jars to target/dependency . 在libraries文件夹中,它会将所有依赖项jar复制到target/dependency By default this will also include jars that are only needed for tests, to exclude these you could use 默认情况下,这还包括仅用于测试的jar,以排除您可以使用的jar

mvn dependency:copy-dependencies -DincludeScope=runtime

Well, there are a couple issues at work here. 那么,这里有几个问题在起作用。 Maven does the hard work of figuring out all the dependencies required for your library to be built and downloads them. Maven努力确定构建和下载库所需的所有依赖项。 These dependencies get stored locally in your Maven repository ( <user home>/.m2/repository ), but unless they are needed as a part of an assembly you will not find them in the target folder. 这些依赖项本地存储在Maven存储库( <user home>/.m2/repository )中,但除非它们作为程序集的一部分需要,否则您将无法在target文件夹中找到它们。 At least, not by default. 至少,不是默认情况下。 What you need to do first is get Maven to store all dependencies in the build folder (this POM excerpt was cribbed from another SO post ): 首先需要做的是让Maven将所有依赖项存储在build文件夹中(这个POM摘录是从另一个SO帖子中删除 ):

<project>
...
    <profiles>
        <profile>
            <id>qa</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-dependency-plugin</artifactId>
                            <executions>
                                <execution>
                                    <phase>install</phase>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                    <configuration>
                                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                    </configuration>
                                </execution>
                            </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

With the POM changes shown above you should now be able to get all the JAR dependencies needed by your library and include them in your classpath (from target/lib ). 通过上面显示的POM更改,您现在应该能够获得库所需的所有JAR依赖项,并将它们包含在类路径中(来自target/lib )。 I would recommend copying them to another folder on your workstation, since the target folder will be nuked every time you execute the clean goal for the libraries Maven build. 我建议将它们复制到工作站上的另一个文件夹,因为每次执行库Maven构建的clean目标时, target文件夹都会被激活。

Now having said all that, why not adapt your project to use Maven as well? 现在说了这么多,为什么不调整你的项目以使用Maven呢? Then all you would need to do is include the top level JAR as a dependency in your POM, and Maven would handle all its sub-dependencies? 那么您需要做的就是将顶级JAR作为POM中的依赖项包含在内,Maven将处理其所有子依赖项? This is the power of Maven after all - it would be to your advantage to leverage it. 毕竟这是Maven的力量 - 利用它对你有利。

In any case, good luck with whichever of the two approaches you select. 无论如何,祝你选择两种方法中的任何一种都好运。

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

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