java 如何使用 maven 打包和运行具有依赖项的简单命令行应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6024027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:01:30  来源:igfitidea点击:

How do I package and run a simple command-line application with dependencies using maven?

javamaven-2maven

提问by mdtsandman

I am brand new to both java and to maven, so this is likely very simple.

我对 java 和 maven 都是全新的,所以这可能非常简单。

If I follow the maven2 hello worldinstructions here:

如果我按照hello world这里的 maven2说明进行操作:

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

everything works OK. If I then alter pom.xmlto bring in a dependency from a remote repository, the files for this dependency get stored in ~/.m2/repository/new-dependency/.

一切正常。如果我随后更改pom.xml以从远程存储库引入依赖项,则此依赖项的文件将存储在~/.m2/repository/new-dependency/.

Using the syntax in the hello worldinstructions to run the application requires that I add the absolute path to the dependency to my classpath (either by setting the environment variable or via the command line switch):

使用说明中的语法hello world来运行应用程序需要我将依赖项的绝对路径添加到我的类路径中(通过设置环境变量或通过命令行开关):

java -cp target/my-app-1.0-SNAPSHOT.jar:/.../.m2/.../new-dependency.jar com.mycompany.app.App

This will obviously get unwieldy quickly :)

这显然会很快变得笨拙:)

I suspect that this is not the usual way of running a java program and that I just need to read more about .jar files, but while I am doing so I would appreciate any tips on how to do this properly.

我怀疑这不是运行 java 程序的常用方式,我只需要阅读更多关于 .jar 文件的信息,但是在我这样做的时候,我希望能得到有关如何正确执行此操作的任何提示。

I am not using an IDE, btw. vim from the command line.

顺便说一句,我没有使用 IDE。vim 从命令行。

Thanks!

谢谢!

Mike.

麦克风。

采纳答案by iruediger

You can make a jar executable by adding the Main-Class attribute to its manifest file. In Maven this is done by the archiver plugin. To add the Main-Class attribute, add this to your pom.xml:

您可以通过将 Main-Class 属性添加到其清单文件来使 jar 可执行文件。在 Maven 中,这是由归档插件完成的。要添加 Main-Class 属性,请将其添加到您的 pom.xml 中:

 <build>
   <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>        
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.mycompany.app.App</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
 </build>

You can now run your jar with the command: java -jar myjar.jaror by double clicking on it (not available in all platforms).

您现在可以使用以下命令运行您的 jar:java -jar myjar.jar或双击它(并非在所有平台上都可用)。

回答by Ryan

You can use maven itself to run it, I believe it sets the classpath for you.

您可以使用 maven 本身来运行它,我相信它会为您设置类路径。

mvn compile

will compile it then you run:

将编译它然后你运行:

mvn exec:java -Dexec.mainClass="com.mycompany.app.App"  

which will execute it.

这将执行它。

You can see http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/for some more info on ways to run (including passing command-line args to the thing you want to run)

您可以查看http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/以获取有关运行方式的更多信息(包括将命令行参数传递给你想跑的东西)

回答by sul

You can use the maven-shade-pluginwhich will create an executable uber war with all dependencies.

您可以使用maven-shade-plugin,它将创建一个具有所有依赖项的可执行 uber war。

OR

或者

Use the appassembler-pluginwhich creates a script that imports all dependencies and lets you execute a main class from the command line.

使用appassembler-plugin创建一个脚本,导入所有依赖项,并让您从命令行执行主类。

回答by dunni

If you want it simple for you and others, then you can generate a jar with all dependencies in it, using the maven-assembly-plugin. Example is here: http://maven.apache.org/plugins/maven-assembly-plugin/usage.html, section Execution: Building an Assembly

如果你想让你和其他人简单,那么你可以使用 maven-assembly-plugin 生成一个包含所有依赖项的 jar。示例在这里:http: //maven.apache.org/plugins/maven-assembly-plugin/usage.html,部分执行:构建程序集