Java 如何从命令行编译具有所有依赖项的 Maven 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38315279/
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
How to compile Maven project from command line with all dependencies?
提问by asdf
I have a Maven project that I can normally compile and run from eclipse but when I compile it from command line it's dependencies are missing and I'm getting errors. I can compile project only after I download dependencies and add them to c:/Java/jdk/jre/lib/ext
我有一个 Maven 项目,我通常可以从 Eclipse 编译和运行它,但是当我从命令行编译它时,它的依赖项丢失了,并且出现错误。我只能在下载依赖项并将它们添加到 c:/Java/jdk/jre/lib/ext 后才能编译项目
How can I compile project and it's dependencies from console line without adding them manually to jdk? Can compiler somehow read maven dependencies?
如何从控制台行编译项目及其依赖项,而无需手动将它们添加到 jdk?编译器可以以某种方式读取 Maven 依赖项吗?
pom.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TCPPing</groupId>
<artifactId>TCPPing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
</project>
采纳答案by wemu
It should be quite straightforward to run your application from an IDE with some maven support (Eclipse, IntellIJ). These IDE's will take care about creating the correct classpath.
从具有一些 maven 支持(Eclipse、IntellIJ)的 IDE 运行您的应用程序应该非常简单。这些 IDE 将负责创建正确的类路径。
If you want to do this manually, try this:
如果您想手动执行此操作,请尝试以下操作:
change to the directory that contains the pom.xml execute the maven command:
切换到包含 pom.xml 的目录执行 maven 命令:
mvn clean install
This will compile your project and create the jar you defined in the pom.xml file. It runs the maven phases clean and every phase up to install (compile, test, etc).
这将编译您的项目并创建您在 pom.xml 文件中定义的 jar。它运行 maven 阶段,并运行每个阶段以进行安装(编译、测试等)。
Then collect all jar files you use as dependencies (required to run your project):
然后收集您用作依赖项的所有 jar 文件(运行项目所需):
mvn dependency:copy-dependencies
This executes the dependency plugin which will copy all dependencies into target/dependency
.
这将执行依赖插件,它将所有依赖复制到target/dependency
.
You can then run your main method using:
然后,您可以使用以下方法运行您的主要方法:
cd target/
java -cp TCPPing-0.0.1-SNAPSHOT.jar:dependency TCPPing
-cp
defines the classpath (all locations / jar files / folders that contain classes). TCPPing
is the class your run that has a main method.
-cp
定义类路径(包含类的所有位置/jar 文件/文件夹)。TCPPing
是您运行的具有 main 方法的类。
Note the :
is for Linux / Mac - I think windows uses a ;
.
请注意:
适用于 Linux / Mac - 我认为 Windows 使用;
.
回答by Tinman
Javac knows nothing about maven. Thus it will not utilize the maven pom.xml.
Javac 对 Maven 一无所知。因此它不会使用 maven pom.xml。
The value of maven is that it removes the manual work of building, testing and releasing a project.
maven 的价值在于它消除了构建、测试和发布项目的手动工作。
This includes getting dependencies, and running javac with them added to the classpath of the javac command.
这包括获取依赖项,并运行 javac,并将它们添加到 javac 命令的类路径中。
You can manually execute javac after maven downloads dependencies to ~/.m2/repository. However you'll need to tell javac where to find the jars. Thus is done via the classpath argument.
maven 将依赖下载到 ~/.m2/repository 后,可以手动执行 javac。但是,您需要告诉 javac 在哪里可以找到罐子。因此是通过类路径参数完成的。
If you are trying to run the project after using mvn to compile it, you'll need to do this in the same folder where your .class files were placed. Thus should be /target/java or similar.
如果您在使用 mvn 编译项目后尝试运行该项目,则需要在放置 .class 文件的同一文件夹中执行此操作。因此应该是 /target/java 或类似的。