java - 从命令行运行带有参数的 Maven 项目会出现错误

标签 java maven

我有一个具有以下文件结构的 Maven 项目:

src/main/java/com/TestFolder/{Java file name}

这个 Java 文件包含一个 main 方法,我需要使用命令行参数来执行该方法。如何做到这一点?请帮忙。

目前正在做这样的事情:

mvn exec:java -Dexec.mainClass=src.main.java.com.TestFolder.MyMainJavaClass -Dexec.args="1"

这是正确的语法吗?因为当我运行这个时,我收到以下错误:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-  plugin:1.4.0:java (default-cli) on project Staples_7Lakh: Execution default-cli of goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java failed: Plugin org.codehaus.mojo:exec-maven-plugin:1.4.0 or one of its dependencies could not be resolved: The following artifacts could not be resolved: backport-util-concurrent:backport-util-concurrent:jar:3.1, org.apache.maven:maven-core:jar:2.2.1, org.codehaus.plexus:plexus-utils:jar:3.0.20: Could not transfer artifact backport-util-concurrent:backport-util-concurrent:jar:3.1 from/to central (https://repo.maven.apache.org/maven2): GET request of: backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar from central failed: SSL peer shut down incorrectly -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

最佳答案

此问题是因为可执行 jar 中缺少依赖项。可执行 jar 需要存在所有运行时依赖项。我也有一个类似的问题,我使用 Maven 程序集插件解决了这个问题,请使用程序集插件配置的 pom 文件找到下面的配置 -

<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>xyz.jeetendra.hibernate.Sample</groupId>
<artifactId>Excercise1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Excercise1</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies> 
<build>
    <finalName>UserService</finalName>
    <plugins>
    <!-- maven Assenmbly Plugin -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
    <configuration>
        <!--get all project dependency -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- Main class in mainfest make a executable jar -->
        <archive>
            <manifest>
                <mainClass>xyz.jeetendra.hibernate.sample.Service</mainClass>
            </manifest>
        </archive>      
    </configuration>
    <executions>
        <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
            </execution>
    </executions>
    </plugin>
</plugins>
</build>

BR

关于java - 从命令行运行带有参数的 Maven 项目会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31619364/

相关文章:

java - android on Text Change Listener

maven - 更改 Maven 运行集成测试的时间

java - 忽略父文件夹中SVN中的文件

java - Jibx根包名称

java - 控制台maven编译错误

maven - .gitignore 文件未复制到原型(prototype) JAR - 解决方法?

eclipse - Maven 到 Eclipse (m2e) 忽略 Kepler 中 POM 文件中的编译器版本设置

java - 如何在服务器应用程序中创建对象并能够从客户端应用程序调用它们

java - BufferedReader readLine() block

java - SQL - 制作表格