简体   繁体   English

mvn exec:java在外部JAR文件中运行java文件

[英]mvn exec:java to run a java file in an external JAR file

In the pom.xml there is a usage of maven-dependency-plugin to download a specific external JAR file to a separate location (in /tmp/externalTestJars/testjar.jar). 在pom.xml中,使用maven-dependency-plugin将特定的外部JAR文件下载到一个单独的位置(在/tmp/externalTestJars/testjar.jar中)。

And I want to use exec-maven-plugin to run a java class in the testjar.jar file (Main.java). 我想使用exec-maven-plugintestjar.jar文件(Main.java)中运行java类。

I found this SO question asking kinda same question but the answer for that question did not help me. 我发现这个问题有点问同样的问题,但这个问题的答案对我没有帮助。

If I directly run the Main.java file (in the original project where the .jar got created, using mvn exec:java ) I can use the below pom configuration. 如果我直接运行Main.java文件(在创建.jar的原始项目中,使用mvn exec:java ),我可以使用下面的pom配置。

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
    <mainClass>org.example.Main</mainClass>
    <!-- need to pass two arguments to the Main.java file main method -->
    <arguments>
        <argument>arg one</argument>
        <argument>arg two</argument>
    </arguments>
</configuration>
</plugin>

In the above SO question it has an answer like below to run a java file inside a .jar file. 在上面的SO问题中,它有一个像下面这样的答案来运行.jar文件中的java文件。

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
    <mainClass>org.example.Main</mainClass>
    <arguments>
        <argument>-jar</argument>
        <argument>/tmp/externalTestJars/testjar.jar</argument>
    </arguments>
</configuration>
</plugin>

But in my case those arguments will be considered as the one to pass for the main method in Main.java sine it is expecting two arguments. 但在我的情况下,这些参数将被视为在Main.java中传递main方法的参数,它正在期待两个参数。 So that approach didn't work for me. 所以这种方法对我不起作用。

Can this be done using exec maven plugin or is there any other method to do the same. 这可以使用exec maven插件完成,还是有任何其他方法来做同样的事情。

If you want to run the class similar to java -cp /tmp/externalTestJars/testjar.jar org.example.Main the plugin should be configured as below. 如果要运行类似于java -cp /tmp/externalTestJars/testjar.jar org.example.Main的类,则应如下配置插件。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-cp</argument>
            <argument>/tmp/externalTestJars/testjar.jar</argument>
            <argument>org.example.Main</argument>
        </arguments>
    </configuration>
</plugin>

If you want to run the class similar to java -jar /tmp/externalTestJars/testjar.jar (assuming org.example.Main is defined as Main-Class in the MANIFEST.MF ) the plugin should be configured as below. 如果要运行类似于java -jar /tmp/externalTestJars/testjar.jar的类(假设org.example.MainMANIFEST.MF定义为Main-Class ),则应如下配置插件。

<configuration>
    <executable>java</executable>
    <arguments>
        <argument>-jar</argument>
        <argument>/tmp/externalTestJars/testjar.jar</argument>
    </arguments>
</configuration>

In both cases run it with mvn exec:exec 在这两种情况下都使用mvn exec:exec运行它

edit: An example for using mvn exec:java . 编辑:使用mvn exec:java的示例。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>org.example.Main</mainClass>
        <additionalClasspathElements>
            <additionalClasspathElement>
                /tmp/externalTestJars/testjar.jar
            </additionalClasspathElement>
        </additionalClasspathElements>
    </configuration>
</plugin>

note: If the project and the jar file testjar.jar both contain the class org.example.Main then the class from the project will be executed. 注意:如果项目和jar文件testjar.jar都包含类org.example.Main那么将执行项目中的类。 As the classpath elements defined by additionalClasspathElement will be appended to the projects classpath. additionalClasspathElement定义的classpath元素将附加到项目类路径。

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

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