【问题标题】:What is the gradle equivalent of maven's exec plugin for running Java apps?用于运行 Java 应用程序的 Maven 的 exec 插件的 gradle 等价物是什么?
【发布时间】:2013-05-03 02:39:14
【问题描述】:

使用 maven,我可以创建一个项目,设置我的 pom 及其依赖项,编写一个带有 main 方法的类,然后运行它:

mvn compile exec:java -Dexec.mainClass=thornydev.App

这样做的 gradle 等价物是什么?

我可以做gradle build,它为我构建了一个 jar 文件,但是如果主类对其他 jar 有任何依赖关系,那么在不设置类路径的情况下仅运行 jar 将无法工作。 gradle java 插件可以运行应用程序并为我设置类路径吗?

我正在寻找一种用于简单一次性使用的命令行解决方案,而不是 IDE 集成(我知道该怎么做)。

【问题讨论】:

    标签: gradle


    【解决方案1】:

    最简单的解决方案是使用Application Plugin,其中提供了run 任务。要使主类可从命令行配置,您必须将 mainClassName 设置为某个系统(或项目)属性的值,然后从命令行传递该属性:

    apply plugin: "application"
    
    mainClassName = System.getProperty("mainClass")
    

    现在您可以使用gradle run -DmainClass=thornydev.App 运行应用程序。

    【讨论】:

    【解决方案2】:

    我需要在构建过程中运行一个 Java 程序,而应用程序插件带来了太多的包袱。

    我确实不喜欢应用程序插件,但最后我使用了“侵入性”要小得多的JavaExec plugin

    我在build.outputDirectory 中有一个类文件MyObfuscator.class,在我有一个像这样的pom.xml 之前,它使用两个参数在构建目录中运行代码混淆:

    <project>
        ...
        <build>
            ...
            <plugins>
                ...
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <id>obfuscate</id>
                            <phase>package</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            
                            <configuration>
                                <executable>java</executable>
                                <workingDirectory>${project.build.outputDirectory}</workingDirectory>
                                <arguments>
                                    <argument>-Djava.library.path=.</argument>
                                    <argument>-classpath</argument>
                                    <argument>${project.build.outputDirectory}:lib.jar</argument>
                                    <argument>MyObfuscator</argument>
                                    <argument>HELLO</argument>
                                    <argument>GOODBYE</argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                ...
            </plugins>
        </build>
        ...
    </project>
    

    我在 Gradle 中把它归结为这个东西:

    apply plugin: "java"
    
    // ...
    
    task obfuscate(type: JavaExec) {
        // Make sure it runs after compilation and resources.
        // Skip this if that's not a requirement.
        dependsOn classes
        
        // run in the buildDir (this requirement was what
        // made a simple "doLast" infeasible)
        workingDir = file(buildDir)
        classpath = files([
            "${buildDir}/classes",
            "${buildDir}/resources/main/lib.jar"
        ])
        main = "MyObfuscator"
    }
    

    如果您需要像上面的 Maven 示例中那样参数化执行,则在任务中添加几行:

    task obfuscate(type: JavaExec) {
        // ... (as above)
        
        // Set PARAM1 to a default value when it's not
        // defined on the command line
        if(!project.hasProperty("PARAM1")) {
            ext.PARAM1 = "HELLO"
        }
        // PARAM1 is dynamic and PARAM2 is static, always "GOODBYE"
        args = [PARAM1, "GOODBYE"]
    }
    

    然后将assemble 任务依赖于obfuscate(将此行放在obfuscate 任务定义下方的任意位置):

    assemble.dependsOn obfuscate
    

    或者让(之前的)jar 任务依赖它。请参阅this docs section here 底部的图表,了解更多关于在何处注入的想法。

    然后您可以像 gradle build -PPARAM1=HELLO 这样调用 gradle 来运行参数化构建。

    【讨论】:

      【解决方案3】:

      如果我的插件是:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>run-benchmarks</id>
            <phase>integration-test</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <classpathScope>test</classpathScope>
              <executable>java</executable>
              <arguments>
                <argument>-classpath</argument>
                <classpath />
                <argument>org.openjdk.jmh.Main</argument>
                <argument>.*</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
      

      gradle 中的等效项是什么?

      【讨论】:

        猜你喜欢
        • 2016-02-10
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 2022-01-11
        • 2021-12-05
        • 1970-01-01
        • 2019-09-14
        • 2018-09-14
        相关资源
        最近更新 更多