gradle 调用 java,如何从Gradle调用静态Java方法

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:

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:

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:

In anotherMethod

arg1 arg2

Thanks

解决方案

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.

import mypackage.MyClass

buildscript {

repositories {

flatDir name: 'localRepository', dirs: 'lib'

}

dependencies {

classpath name: 'MQMonitor'

}

}

task myTaskCallJava << {

MyClass.foo()

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值