如何从命令行编译Maven项目与所有依赖项? [英] How to compile Maven project from command line with all dependencies?

查看:85
本文介绍了如何从命令行编译Maven项目与所有依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven项目,我通常可以从eclipse编译和运行,但是当我从命令行编译它时它的依赖项丢失了,我收到了错误。我可以在下载依赖项之后编译项目并将它们添加到c:/ Java / jdk / jre / lib / ext

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

如何从控制台编译项目及其依赖项行没有手动添加到jdk?编译器能以某种方式读取maven依赖吗?

How can I compile project and it's dependencies from console line without adding them manually to jdk? Can compiler somehow read maven dependencies?

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>


推荐答案

从应用程序运行应用程序应该非常简单具有一些maven支持的IDE(Eclipse,IntellIJ)。这些IDE将负责创建正确的类路径。

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.

如果您想手动执行此操作,请尝试以下操作:

If you want to do this manually, try this:

切换到包含pom.xml的目录
执行maven命令:

change to the directory that contains the pom.xml execute the maven command:

mvn clean install

这将编译您的项目并创建您在pom.xml文件中定义的jar。它运行maven阶段干净,每个阶段都要安装(编译,测试等)。

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).

然后收集你用作依赖项的所有jar文件(运行你的项目所需) ):

Then collect all jar files you use as dependencies (required to run your project):

mvn dependency:copy-dependencies

这将执行依赖插件,它将所有依赖项复制到 target / dependency

This executes the dependency plugin which will copy all dependencies into target/dependency.

然后你可以使用以下方法运行你的main方法:

You can then run your main method using:

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

-cp 定义类路径(包含类的所有位置/ jar文件/文件夹)。 TCPPing 是具有main方法的运行类。

-cp defines the classpath (all locations / jar files / folders that contain classes). TCPPing is the class your run that has a main method.

注意适用于Linux / Mac - 我认为Windows使用;

Note the : is for Linux / Mac - I think windows uses a ;.

这篇关于如何从命令行编译Maven项目与所有依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆