简体   繁体   English

如何从命令行编译Maven项目与所有依赖项?

[英]How to compile Maven project from command line with all dependencies?

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. 我有一个Maven项目,我通常可以编译并从eclipse运行但是当我从命令行编译它时它的依赖项丢失了,我收到了错误。 I can compile project only after I download dependencies and add them to c:/Java/jdk/jre/lib/ext 我可以在下载依赖项之后编译项目,并将它们添加到c:/ Java / jdk / jre / lib / ext

How can I compile project and it's dependencies from console line without adding them manually to jdk? 如何从控制台行编译项目及其依赖项,而无需手动将其添加到jdk? Can compiler somehow read maven dependencies? 编译器可以以某种方式读取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>

It should be quite straightforward to run your application from an IDE with some maven support (Eclipse, IntellIJ). 从具有一些maven支持的IDE(Eclipse,IntellIJ)运行应用程序应该非常简单。 These IDE's will take care about creating the correct classpath. 这些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. 这将编译您的项目并创建您在pom.xml文件中定义的jar。 It runs the maven phases clean and every phase up to install (compile, test, etc). 它运行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: 然后,您可以使用以下命令运行main方法:

cd target/
java -cp TCPPing-0.0.1-SNAPSHOT.jar:dependency TCPPing

-cp defines the classpath (all locations / jar files / folders that contain classes). -cp定义类路径(包含类的所有位置/ jar文件/文件夹)。 TCPPing is the class your run that has a main method. TCPPing是具有main方法的运行类。

Note the : is for Linux / Mac - I think windows uses a ; 注意:适用于Linux / Mac - 我认为Windows使用的是; .

Javac knows nothing about maven. Javac对maven一无所知。 Thus it will not utilize the maven pom.xml. 因此它不会使用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一起添加到javac命令的类路径中。

You can manually execute javac after maven downloads dependencies to ~/.m2/repository. 您可以在maven下载依赖关系到〜/ .m2 / repository之后手动执行javac。 However you'll need to tell javac where to find the jars. 但是你需要告诉javac在哪里找到罐子。 Thus is done via the classpath argument. 这样就可以通过classpath参数完成。

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. 如果您在使用mvn编译它之后尝试运行该项目,则需要在放置.class文件的同一文件夹中执行此操作。 Thus should be /target/java or similar. 因此应该是/ target / java或类似的。

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

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