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

守給你的承諾、 提交于 2019-12-06 04:05:39

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.

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

<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

edit: An example for using 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. As the classpath elements defined by additionalClasspathElement will be appended to the projects classpath.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!