简体   繁体   English

从命令行运行带有参数的Maven项目给出错误

[英]Run Maven project from command line with arguments give error

I am having a Maven project that has following file structure : 我有一个具有以下文件结构的Maven项目:

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

This Java file contains a main method that I need to execute with argument from command line. 这个Java文件包含一个主要方法,我需要使用命令行中的参数来执行。 How this can be done ? 如何做到这一点? Please help. 请帮忙。

Currently am doing something like this : 目前正在做这样的事情:

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

Is this right syntax to do it ? 这是正确的语法吗? Because when i run this i get following error : 因为当我运行此程序时,出现以下错误:

[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

This problem is because the dependancies are missing in the executable jar. 此问题是因为可执行jar中缺少相关性。 The executable jar needs all run time dependencies to be present. 可执行jar需要所有运行时相关性都存在。 I also had a similar problem which I solved using maven assembly plugin please find below configuration with the pom file for the assembly plugin configuration - 我也有一个类似的问题,我使用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 BR

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

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