如何从 Gradle 调用静态 Java 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17590581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I call a static Java method from Gradle
提问by danielbeddoe
I have a gradle build script which currently works by simply executing a Java class through it's main method. What I want to know is, how can I call a static method in the same class but not have to go through the main method. The current gradle code is as follows:
我有一个 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'
}
And my Java class as follows:
我的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);
}
}
This gives me the output:
这给了我输出:
: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
My question is simply how can I skip the main method, and call the method "anotherMethod" directly from the gradle script? The output would then simply be:
我的问题只是如何跳过主要方法,并直接从 gradle 脚本调用方法“anotherMethod”?输出将只是:
In anotherMethod
arg1 arg2
Thanks
谢谢
回答by user3271098
you have to add the jar or class to the classpath. here is an example with a jar file who contains the class.
Inside the file build.gradle add the dependencies.
My jar file is in the lib
folder the path is lib/MQMonitor.jar
.
您必须将 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()
}
回答by X.HE
I have been working on this too. You know I like the function of eclipse and intellij with the run with Junit options, and I want to do this using command line and gradle.
我也一直在做这方面的工作。你知道我喜欢使用 Junit 选项运行的 eclipse 和 intellij 功能,我想使用命令行和 gradle 来做到这一点。
If you could accept putting your test method in the directory of 'test' directory of gradle. I actually have a fair solution.
如果您可以接受将您的测试方法放在 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");
}
}
This my test class which is in src/test/java/com/udacity/gradle/TestClass.java
这是我在src/test/java/com/udacity/gradle/TestClass.java 中的测试类
Then the under below is the file of my build.gradle
然后下面是我的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"
}
}
Simple idea you know this is a test class so I use the test task of gradle. And to specify which method to use, I added a Test filter which could specify down to method.
简单的想法你知道这是一个测试类,所以我使用 gradle 的测试任务。为了指定使用哪种方法,我添加了一个可以指定方法的测试过滤器。
Then you could just run
那你就可以跑了
gradle test
Then you could find in console that you have what you want in there. However, remember to add
然后你可以在控制台中找到你想要的东西。不过记得加
testLogging.showStandardStreams = true
if you don't do this, gradle would swallow your console output. But even if you don't add this line. You could read a test log in the directory of
如果你不这样做,gradle 会吞下你的控制台输出。但即使你不添加这一行。您可以在以下目录中读取测试日志
...../build/test-results/test/TEST-com.udacity.gradle.TestClass.xml
...../build/test-results/test/TEST-com.udacity.gradle.TestClass.xml
There are well organized test reports output in it.
其中有组织良好的测试报告输出。
<?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>
回答by Abhijit Sarkar
Assuming the class is on the buildscript classpath (it should be, since you're calling main
from the same class)
假设该类在 buildscript 类路径上(应该是,因为您是main
从同一个类调用的)
task runSimple {
doLast {
com.test.model.JavaTest.anotherMethod("foo", "bar")
}
}
Tested on Gradle 4.6
在 Gradle 4.6 上测试
回答by Adam R.
If you want to execute a static method you will need to add the class to the Gradle build script's classpath.
如果要执行静态方法,则需要将该类添加到 Gradle 构建脚本的类路径中。
To add the code to the build scripts classpath if your code is in a repository:
如果您的代码在存储库中,要将代码添加到构建脚本类路径:
buildscript {
repositories {
maven { url "${yourRepositoryURL}" }
}
dependencies {
classpath 'com.yourgroup:yourpackagename:version'
}
}
To add the code to the build scripts classpath if you code is built locally (I didn't test this one):
如果您的代码是在本地构建的,则要将代码添加到构建脚本类路径中(我没有测试过这个):
buildscript {
dependencies {
classpath files("path/to/where/the/class/files/are")
}
}
Then you should be able to call that method just like any other:
然后您应该能够像调用其他方法一样调用该方法:
task runSimple(dependsOn: 'classes') {
doFirst() {
com.test.model.JavaTest.anotherMethod('arg1', 'arg2')
}
}