Java 如何编写Maven脚本,在项目中的代码打包后调用Jar文件?

Java 如何编写Maven脚本,在项目中的代码打包后调用Jar文件?,java,maven,Java,Maven,其思想是像往常一样运行“mvn包”,在完成所有步骤之后,应该调用Jar实用程序,将打包代码(Jar或war文件)的文件路径作为参数传递 从命令行按如下方式调用该实用程序: java-jar Utility.jar-filepath{新jar/war文件的路径} 我想将最后一步集成到构建过程中。如何修改pom.xml文件以实现此目的?请查看。您可以将它的执行绑定到包阶段(将在包定义的内置绑定之后运行),以使用参数“-jar Utility.jar-filepath${project.build.d

其思想是像往常一样运行“mvn包”,在完成所有步骤之后,应该调用Jar实用程序,将打包代码(Jar或war文件)的文件路径作为参数传递

从命令行按如下方式调用该实用程序:

java-jar Utility.jar-filepath{新jar/war文件的路径}

我想将最后一步集成到构建过程中。如何修改pom.xml文件以实现此目的?

请查看。您可以将它的执行绑定到包阶段(将在包定义的内置绑定之后运行),以使用参数“-jar Utility.jar-filepath${project.build.directory}/${project.artifactId}-${project.version}-${project.packaging}”运行java(可执行文件) 结果看起来有点像这样:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>run jar utility</id>
                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>java</executable>
                            <arguments>
                                <argument>-jar</argument>
                                <argument>Utility.jar</argument>
                                <argument>-filepath</argument>
                                <argument>${project.build.directory}/${project.build.finalName}.${project.packaging}</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

org.codehaus.mojo

)这里有另一种方法来运行@radai建议的exec maven插件。如果你可以这样做,我推荐你

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <inherited>false</inherited>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!--I don't want my project's dependencies in the classpath-->
                <includeProjectDependencies>false</includeProjectDependencies>
                <!--Just include the dependencies that this plugin needs.  IE: the Utility dependencies-->
                <includePluginDependencies>true</includePluginDependencies>
                <executableDependency>
                    <groupId>com.utility</groupId>
                    <artifactId>Utility</artifactId>
                </executableDependency>
                <mainClass>com.utility.MyMainClass</mainClass>
                <!--You may be able to use a variable substitution for pathToJarFile.  Try it and see-->
                <commandlineArgs>-filepath pathToJarFile</commandlineArgs>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.utility</groupId>
                    <artifactId>Utility</artifactId>
                    <version>1.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>

org.codehaus.mojo
execmaven插件
1.2.1
假的
JAVA
假的
真的
com.utility
效用
com.utility.MyMainClass
-文件路径pathToJarFile
com.utility
效用
1.0-快照