簡體   English   中英

如何從Gradle調用靜態Java方法

[英]How do I call a static Java method from Gradle

我有一個gradle構建腳本,當前可以通過簡單地通過它的main方法執行Java類來工作。 我想知道的是,如何在同一個類中調用靜態方法,而不必遍歷main方法。 當前的gradle代碼如下:

import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'java'

defaultTasks 'runSimple'

project.ext.set("artifactId", "test-java")

File rootDir = project.getProjectDir()
File targetDir = file("${rootDir}/target")
FileCollection javaClasspath = files("${targetDir}/tools.jar")

task(runSimple, dependsOn: 'classes', type: JavaExec) {
    main = 'com.test.model.JavaTest'
    classpath = javaClasspath
    args 'arg1'
    args 'arg2'
}

而我的Java類如下:

package com.test.model;

public class JavaTest {

    public static void main(String[] args) throws Exception {
        System.out.println("In main");
        anotherMethod(args[0], args[1]);
    }

    public static void anotherMethod(String arg1, String arg2) {
        System.out.println("In anotherMethod");
        System.out.println(arg1 + " " + arg2);
    }
}

這給了我輸出:

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runSimple
In main
In anotherMethod
arg1 arg2

BUILD SUCCESSFUL

Total time: 2.344 secs

我的問題只是簡單地如何跳過main方法,而直接從gradle腳本中調用方法“ anotherMethod”? 輸出將簡單地是:

In anotherMethod
arg1 arg2

謝謝

您必須將jar或類添加到類路徑。 這是一個包含該類的jar文件的示例。 在文件build.gradle內添加依賴項。 我的jar文件位於lib文件夾中,路徑為lib/MQMonitor.jar

import mypackage.MyClass
buildscript {
   repositories {
      flatDir name: 'localRepository', dirs: 'lib'
   }
    dependencies {
        classpath name: 'MQMonitor'
    }
}

task myTaskCallJava << {
   MyClass.foo()
}

我也一直在努力。 您知道我喜歡通過Junit選項運行eclipse和intellij的功能,我想使用命令行和gradle進行此操作。

如果您可以接受將您的測試方法放在gradle的“ test”目錄中。 我實際上有一個公平的解決方案。

package com.udacity.gradle;
import org.junit.Test;

    public class TestClass {
        @Test
        public void anotherMethod() {
            System.out.println("This is it, I want this!!!");
        }
        @Test
        public void notMyWantedMethod1() {

            System.out.println("not my wanted");
        }

        public void notMyWantedMethod2() {
            System.out.println("not my wanted");
        }

    }

這是我的測試類,位於src / test / java / com / udacity / gradle / TestClass.java中

然后下面的是我的build.gradle的文件

apply plugin: "java"
repositories {
    mavenCentral()
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}


test {
    testLogging.showStandardStreams = true
    filter {
        //include specific method in any of the tests
        includeTestsMatching "*.TestClass.anotherMethod"
    }
}

一個簡單的想法,您知道這是一個測試類,因此我使用gradle的測試任務。 為了指定要使用的方法,我添加了一個測試過濾器,可以指定方法。

那你就可以跑

gradle test

然后,您可以在控制台中找到所需的內容。 但是,請記住添加

testLogging.showStandardStreams = true

如果您不這樣做,gradle將吞噬您的控制台輸出。 但是,即使您不添加此行。 您可以在以下目錄中閱讀測試日志

..... / build / test-results / test / TEST-com.udacity.gradle.TestClass.xml

其中有組織良好的測試報告輸出。

<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="com.udacity.gradle.TestClass" tests="1" skipped="0" failures="0" errors="0" timestamp="2018-03-31T19:26:44" hostname="hexin-PC" time="0.022">
  <properties/>
  <testcase name="anotherMethod" classname="com.udacity.gradle.TestClass" time="0.022"/>
  <system-out><![CDATA[This is it, I want this!!!
]]></system-out>
  <system-err><![CDATA[]]></system-err>
</testsuite>

如果要執行靜態方法,則需要將類添加到Gradle構建腳本的類路徑中。

要將代碼添加到構建腳本的類路徑中(如果代碼位於存儲庫中):

buildscript {
    repositories {
        maven { url "${yourRepositoryURL}" }
    }
    dependencies {
        classpath 'com.yourgroup:yourpackagename:version'
    }
}

如果您的代碼是本地構建的,則將代碼添加到構建腳本的類路徑中(我沒有測試這一步):

buildscript {
    dependencies {
        classpath files("path/to/where/the/class/files/are")
    }
}

然后,您應該能夠像其他方法一樣調用該方法:

task runSimple(dependsOn: 'classes') {
    doFirst() {
        com.test.model.JavaTest.anotherMethod('arg1', 'arg2')
    }
}

假設該類位於buildscript類路徑上(應該是,因為您要從同一類調用main

task runSimple {
  doLast {
    com.test.model.JavaTest.anotherMethod("foo", "bar")
  }
}

在Gradle 4.6上測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM