maven exec:jar中的java运行类文件

cod*_*lid 7 maven

我将我的代码打包到jar中

罐子打包好了.

jar -tfv target/test-1.0-SNAPSHOT.jar

com/
com/codevalid/
com/codevalid/App.class
log4j.xml
META-INF/maven/com.codevalid/test/pom.xml
META-INF/maven/com.codevalid/test/pom.properties
Run Code Online (Sandbox Code Playgroud)

当它们作为单独的类文件存在时,我可以执行它们 exec:java

如何使用maven在jar中运行类文件exec:java

cod*_*lid 6

好的,这就是我最终做的事情.
我用它制作了罐子

mvn assembly:single
Run Code Online (Sandbox Code Playgroud)

和使用

java -jar ./target/App-1.0-SNAPSHOT-jar-with-dependencies.jar com.codevalid.App
Run Code Online (Sandbox Code Playgroud)

我确实看到了一个我可以使用的替代方案

mvn exec:java -Dexec.mainClass="com.codevalid.App"
Run Code Online (Sandbox Code Playgroud)

但我不确定如何将jar的名称作为类路径传递


kap*_*pex 5

exec:java您可以通过添加一些内容来使用目标运行 jar 文件arguments

<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>target/myJar-1.0-SNAPSHOT.jar</argument>
        </arguments>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

如果您有一个可执行 jar 并且不想定义入口点,则需要设置executable并使用exec:exec目标:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-jar</argument>
            <argument>target/myJar-1.0-SNAPSHOT.jar</argument>
        </arguments>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)


nwi*_*ler 3

您需要将 jar 文件作为 exec 插件的依赖项包含在内,例如:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
  <execution>
    <phase>install</phase>
    <goals>
      <goal>java</goal>
    </goals>
    <configuration>
      <mainClass>com.codevalid.App</mainClass>
    </configuration>
  </execution>
</executions>
<dependencies>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

dependency如果该类com.codevalid.App是作为当前项目的一部分进行编译的,则可以跳过声明。