简体   繁体   English

运行具有许多依赖项的Maven项目

[英]Running a Maven project with many dependencies

I'm hacking on a Maven -based project with a lot of dependencies; 我正在研究一个基于Maven的项目, 项目具有很多依赖关系。 the project is normally meant to be developed in Eclipse but I need to work on it from the command line. 该项目通常是要在Eclipse中开发的,但我需要从命令行进行处理。

How to build+execute the project in a sane way? 如何以理智的方式构建和执行项目? Something like mvn run , but of course Maven is not meant for running Java projects (for some reason). 类似于mvn run ,但是Maven当然不是用于运行Java项目的(出于某种原因)。

The problem is specifying all the dependencies on java 's commandline, I don't even know how to autogenerate that. 问题是在java的命令行上指定了所有依赖项,我什至不知道如何自动生成它。 I can currently deal with it using the assembly:single maven plugin (using the jar-with-dependencies descriptor) which will package the dependencies to a single .jar for me. 我目前可以使用assembly:single maven插件(使用jar-with-dependencies描述符)来处理它,它将为我将依赖项打包到单个.jar中。

However, there really is a lot of dependencies and the assembly phase can take about two minutes, greatly disrupting my hack-test cycles so I'm looking for other ways to run the project with minimum build overhead - any recommendations, please? 但是,确实存在很多依赖关系,并且组装阶段可能需要两分钟左右的时间,这极大地破坏了我的hack-test周期,因此我正在寻找其他方法以最小的构建开销来运行项目-请提出任何建议?

Note: One possibility is running it in Eclipse once and capturing the java commandline. 注意:一种可能是在Eclipse中运行一次并捕获Java命令行。 However, that's just a one-time hack, not a general solution in case I change pom.xml later or come to another project from the suite without Eclipse access anymore. 但是,这只是一次性的技巧,不是通用的解决方案,以防我稍后更改pom.xml或从套件中转到另一个项目而不再使用Eclipse的情况。

Have a look at the maven exec plugin 看看maven exec插件

mvn exec:java -Dexec.mainClass="com.example.Main"

if you do this frequently, you can of course configure it via plugin configuration. 如果您经常这样做,则当然可以通过插件配置进行配置。

See this question: How can I create an executable JAR with dependencies using Maven? 看到这个问题: 如何使用Maven创建具有依赖关系的可执行JAR? . You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest. 您可以在包阶段之前使用dependency-plugin在单独的目录中生成所有依赖项,然后将其包含在清单的类路径中。

Regarding finding out project dependencies - you can use maven dependency plugin 关于查找项目依赖项-您可以使用Maven依赖项插件

http://maven.apache.org/plugins/maven-dependency-plugin/list-mojo.html http://maven.apache.org/plugins/maven-dependency-plugin/list-mojo.html

If you want to put them into file it'd be smth like 如果你想把它们归档,就像

mvn dependency:list > dependencies.txt

I see three solution to this: 我看到了三种解决方案:

  • onejar-maven-plugin - faster than assemlby with jar-with-dependencies descriptor onejar-maven- plugin-使用jar-with-dependencies描述符比assemlby更快

    With onejar-maven-plugin, you'll (...) get a nice clean super jar with the dependency jars inside. 使用onejar-maven-plugin,您将获得一个不错的干净的超级jar,其中包含依赖项jar。

  • Spring Boot Maven Plugin - but this is dedicated to Spring projects Spring Boot Maven插件 -但这专用于Spring项目

  • Maven Assembly Plugin with custom descriptor. 具有自定义描述符的Maven程序集插件 This custom descriptor should grab all dependencies into lib folder, maven-jar-plugin should set Class-Path in Manifest.fm according to this new location. 此自定义描述符应将所有依赖项都捕获到lib文件夹中,maven-jar-plugin应根据此新位置在Manifest.fm中设置Class-Path After this you can simply execute your program or zip your jar with lib folder as distribution. 之后,您可以简单地执行您的程序或使用lib文件夹压缩jar。

After this is possible to run your program on your computer or any other with one command: 之后,可以使用一个命令在计算机或任何其他计算机上运行程序:

java -jar myjar.jar

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

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