Use Java class in Gradle build script - java

I have a Gradle build script which has to instantiate a Java class in a Task and call a method on the created object. Currently, I have the following:
apply plugin: 'java'
dependencies {
compile files("libs/some.library.jar")
}
task A << {
def obj = new some.library.TestClass()
obj.doSomething()
}
The problem is that the class some.library.TestClass() is not found. I read this article about how to use Groovy classes in Gradle, but I need my Java class to come from an external JAR file. How can I add a jar to the build source? It seems that the dependencies block doesnt do what I expect it to do. Can anyone give me a hint in the right direction?

The dependency compile files("libs/some.library.jar") is added as a project dependency not as the script dependency itself. What You need to do is to add this dependency in script's classpath scope.
apply plugin: 'java'
buildscript {
dependencies {
classpath files("libs/some.library.jar")
}
}
task A << {
def obj = new some.library.TestClass()
obj.doSomething()
}
Now it should work.

Related

How to simulate a gradle build script in junit test?

I am playing a little bit with my own gradle plugin (for gradle 6.5.1). Now I wrote a small test (implemented in java) which is not working:
Project project = ProjectBuilder.builder().build();
ScriptHandler buildscript = project.getBuildscript();
Action<? super MavenArtifactRepository> action = new Action<MavenArtifactRepository>() {
#Override
public void execute(MavenArtifactRepository mavenArtifactRepository) {
mavenArtifactRepository.setUrl("https://plugins.gradle.org/m2/");
}
};
buildscript.getRepositories().maven(action);
buildscript.getDependencies().add("classpath", "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE");
project.getPlugins().apply("java");
project.getPlugins().apply("io.spring.dependency-management");
It says Plugin with id 'io.spring.dependency-management' not found. I thought that I copied the original from https://plugins.gradle.org/plugin/io.spring.dependency-management
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
But I guess thats not the case. :-) Any idea how to translate that snippet to java world?
If you want to apply an other plugin in your test you need the other plugin on your classpath. So you have to add to the build file of your gradle-plugin and than its there.
In this case it means write the following to your build.gradle
testImplementation "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"

Execute commons-math Java class from a gradle task

I need to execute from a gradle task classes included on a jar file.
For instance I would like to create a gradle task able to execute the class FastMath(http://commons.apache.org/proper/commons-math/download_math.cgi).
The current build.gradle script is the following:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile files('/pathToJars/commons-math3-3.6.1.jar')
}
task t1 {
doLast {
println FastMath.abs(3)
}
}
I get this error message:
What went wrong:
Execution failed for task ':t1'.
Could not get unknown property 'FastMath' for task ':t1' of type org.gradle.api.DefaultTask.
I understand I am missing the class import, for instance adding the following statement but I do not know how or where:
import org.apache.commons.math3.util.FastMath;
I am wondering what I am doing wrong or how the script has to be configured. Any suggestion will be appreciated.
EDIT 1: The code that actually works is the following:
apply plugin:'java'
import org.apache.commons.math3.util.FastMath;
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.apache.commons:commons-math3:3.6.1'
}
}
task t1 {
doLast {
println FastMath.abs(3)
}
}
Well, you have multiple issues in your buildscript.
If you use mavenCentral() as repository, use the dependencies from there, you will gain transitive dependency resolution and automatic conflict resolution and you do not have to keep the libs in your VCS or wherever, so compile files('/pathToJars/commons-math3-3.6.1.jar') should actually be compile 'org.apache.commons:commons-math3:3.6.1'.
As you correctly noted, you either have to use fully-qualified class names or imports, so either, replace println FastMath.abs(3) by println org.apache.commons.math3.util.FastMath.abs(3), or add import org.apache.commons.math3.util.FastMath anywhere before. Typically this is done top-most in a file, just like for Java too.
Even if you would do both, it would not work, because you add the dependency to the compile classpath of your actual project. Instead you need it in the classpath of your buildscript, so you need to move the dependencies block inside a buildscript block and if you followed advice 1, then of course also the repositories block. You also need classpath instead of compile in the dependency declaration.

How to override a gradle build with dev/prod configuration

I'm evaluating gradle for replacing an ant build script and I can't manage to find a solution for creating a standard build script that correctly manages dev/prod environment.
Than ant script (it's for a java project, not android) is structured in this way:
a common script with the standard tasks (compile, build-jar, build-war)
a specific project script that includes the first one and through some properties it defines where the war task should pick the correct files
Our project structure/taks allows to override entire directories in the final war. Let consider this example:
the dev configuration is the standard one and lays int the dir webcontent
there are multiple prod conf (one of each specific installation, we do not have more that 10 different prod configs) all under the prod dir (i.e. *prod/conf1*m prod/conf2, etc)
The ant build has the dev_build task as the prod_conf1_build one, the prod_conf2_build one ,etc
the XXX_build task do the same things:
specify the parent (it's a project property) dir that contains the env dir/files
call the same ant taks that build the war using the property specified in the calling task
I'm trying to do the same in gradle but it seems that even calling a taks from another one it creates some problem (i.e. the task is always up to date)
Here is the script (it's a working draft, I'm learning gradle) that tries to do the same but it's not working when I call war_prod the taks does nothing since it reports up-to-date
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
project.ext.envdir = ""
eclipse {
jdt {
sourceCompatibility = 1.8
targetCompatibility = 1.8
javaRuntimeName = "jdk-1.8.x"
}
}
// In this section you declare where to find the dependencies of your project
repositories {
maven {
url 'http://artifactory.zzzz.priv/artifactory/libs-release'
url 'http://artifactory.zzzz.priv/artifactory/libs-snapshot'
credentials {
username 'xxxx'
password 'yyyy'
}
}
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.18'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
task war_prod {
project.ext.envdir='prod/conf1'
project.ext.envdir=project.ext.envdir.replaceAll('\\\\',File.pathSeparator)
project.ext.envdir=project.ext.envdir.replaceAll('/',File.pathSeparator)
tasks.war.execute()
}
war {
eachFile {
println 'endir' + project.ext.envdir
println 'evaluating' + it
FileTree tree = fileTree(dir: project.ext.envdir)
tree.visit { FileVisitDetails file->
if (!file.file.isDirectory()) {
println '\tFileVisitDetails relpath ' + file.relativePath
println '\tsourcepath ' + it.file.getAbsolutePath()
println '\tcontains ' + it.file.getAbsolutePath().contains(project.ext.envdir)
if (it.relativePath == file.relativePath && !it.file.getAbsolutePath().contains(project.ext.envdir)) {
it.exclude()
println '\texcluding ' + it
} else {
if (it!=null) {
//println '\tincluding ' + it
}
}
}
}
}
from 'prod/conf1'
}
Can anyone point me in the right direction for creating a correct gradle script?
Is there a specific gradle way to build war files with prod/dev configurations (where the configuration is represented by some dir and files)?
In such scenarios task rules might be very useful. Basic idea is to keep configurations in a structured way and use a general task to build a war file with a configuration defined. Please have a look at build.gradle below:
apply plugin: 'war'
repositories {
mavenCentral()
}
tasks.addRule("Pattern: buildWar<ENV>") { String taskName ->
if (taskName.startsWith('buildWar')) {
def env = (taskName - 'buildWar').toLowerCase()
if (env in ['dev', 'prod',]) {
task(taskName, type: War) {
println "Configuring env: $env"
from("src/main/conf/$env") {
into("conf")
}
}
} else {
println "Invalid env: $env, skipping."
}
}
}
The buildWarENV rule defined here is pretty self descriptive. It accepts two environments dev and prod and prepares war file by taking configuration from appropriate folder. You can find a demo here. In case of questions, just ask.
P.S. Gradle has a bit different working model than ant, start with the basics. And what's important, never run a task from within other task.

intellij build jar artifact containing gradle dependencies

I basically want to do something simple - or atleast i think it should be pretty simple.
My goal is to create an Intellij gradle project, add some dependencies to the module using gradle and add some java source code to it.
Then I just want to have an option to somehow compile the whole thing into 1 jar, containing all grade dependencies and being able to execute using "java -jar"
However it turned out that this is not as easy is i had thought.
I just created a new gradle project from intellij and added a Main class.
I´ll give you an overview over my files:
settings.gradle:
rootProject.name = 'gradleTestNewJar'
build.gradle:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
sourceCompatibility = 1.6
version = '1.0'
repositories {
mavenCentral()
}
mainClassName = "com.randomPackage.StarterClass"
dependencies {
compile 'org.seleniumhq.selenium:selenium-java:2.46.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
main class:
package com.randomPackage;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class StarterClass {
public static void main(String[] args){
System.out.println("test");
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38);
driver.quit();
}
}
The main method of "MyStart" is executed when running from Intellij via debug.
So it works, when all dependencies get loaded correctly.
NOTE: I use Intellij Community Edition if this makes any difference.
What i tried:
1. I tried to just use "gradlew clean build".
This created a jar, but without libs.
But I didn´t expect it to be as easy as this.
2. I tried to build an artifact of the module as suggested here:
http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
I tried it with extracted and not extracted dependencies.
In both cases the dependencies were added into the jar, but they were added to the root of the jar.
When i tried to run the jar file via "java -jar", it complained:
"Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
..."
OK, so it couldn´t load the dependencies.
NOTE: I thought that the dependencies were not added to the classpath, but i am not sure about this. However, i would expect Intellij to add dependencies to the classpath( or declare in the manifest file)
3. I also tried to use the gradle application plugin.
However this creates a zip/tar which contains a execute script and a bin folder which was not my intention.
So i started googling for hours and hours but i cann´t find a solution to my problem.
Come on this cannot be so hard - it is just so basic.
I am sure some genius can help me out and point me to my - probably stupid - failure.
My current solution is as follows:
I use gradle to build a jar containing all libs, I do this witha custom task called fatJar.
Here is a part from my build.gradle
apply plugin: 'java'
jar {
manifest {
attributes("Manifest-Version": "1.0",
"Main-Class": "com.randomPackage.MainClass");
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
Then I just execute "gradle fatJar" on the command line and get a perfect jar.

Creating a Gradle Custom Plugin with Java

I'm creating a build process with Gradle and I want to provide a plugin that uses Java code. The Gradle plugin documentation says this is possible:
You can implement a custom plugin in any language you like, provided the implementation ends up compiled as bytecode. For the examples here, we are going to use Groovy as the implementation language. You could use Java or Scala instead, if you want.
However, after multiple hours of Googling and reading, I have yet to find any explanation of how to create a Gradle custom plugin with Java. It seems like you could create the code for it in a directory like:
<rootProjectDir>/buildSrc/src/main/java/
MyGradlePlugin.java
MyGradleTasks.java
But the question then becomes:
How to implement the plugin class and tasks in Java to be compatible with Gradle?
How to get Gradle to recognize the Java classes and tasks so you can use them in a build?
Can you just reference the plugin class like you do for the Groovy equivalent?
src/main/resources/META-INF/gradle-plugins/myjavaplugin.properties
implementation-class=org.me.MyGradlePlugin
I realize that I could just call the Java code with project.javaexec or JavaExec, but I'm concerned this will make my build process less portable.
Here's a basic, stand-alone, Java-based Gradle plugin and the steps to get it working:
Make sure Gradle 1.6 or higher and Java JDK is installed
Create these files below with the directory structure indicated
Change directories to the <projectRoot>/plugin directory
Execute the plugin build: $ gradle uploadArchives This (very important) step compiles the Java code and puts it in your local Maven repo (../repo).
Now execute the consumer script by changing directories to <projectRoot>/consumer
Execute the script that depends on the plugin: $ gradle checkitout
Java Classes
projectRoot/plugin/src/main/java/org/joefernandez/gradle/MyJavaPlugin.java
package org.joefernandez.gradle;
import org.gradle.api.Project;
import org.gradle.api.Plugin;
public class MyJavaPlugin implements Plugin<Project> {
#Override
public void apply(Project target) {
target.task("javaTask");
}
}
projectRoot/plugin/src/main/java/org/joefernandez/gradle/MyJavaTask.java
package org.joefernandez.gradle;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
public class MyJavaTask extends DefaultTask {
#TaskAction
public void javaTask() {
System.out.println("Hello from MyJavaTask");
}
}
Plugin Class declaration
projectRoot/plugin/src/main/resources/META-INF/gradle-plugins/test-plugin.properties
implementation-class=org.joefernandez.gradle.MyJavaPlugin
Plugin Build Script
Note the uploadArchives task: You must run this task to make the plugin available to the consumer script.
projectRoot/plugin/build.gradle
apply plugin: 'java'
dependencies {
compile gradleApi()
}
apply plugin: 'maven'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
}
group = 'org.joefernandez'
version = '1.0-SNAPSHOT'
uploadArchives {
repositories {
mavenDeployer {
repository(url: uri('../repo'))
}
}
}
Settings for the Plugin
projectRoot/plugin/settings.gradle
rootProject.name = 'MyJavaPlugin'
Root script
projectRoot/build.gradle
apply plugin: 'java'
dependencies {
compile gradleApi()
}
Consumer script
projectRoot/consumer/build.gradle
buildscript {
repositories {
maven {
url uri('../repo')
}
}
dependencies {
classpath group: 'org.joefernandez',
name: 'MyJavaPlugin',
version: '1.0-SNAPSHOT'
}
}
apply plugin: 'test-plugin'
task checkitout(type: org.joefernandez.gradle.MyJavaTask) {
println("running consumer task!")
}
I would like to propose a few amendments to Joe's great answer.
projectRoot/plugin/src/main/java/org/joefernandez/gradle/MyJavaPlugin.java
This change wires the task into the supplied project without the need to sub-class it.
package org.joefernandez.gradle;
import org.gradle.api.Project;
import org.gradle.api.Plugin;
public class MyJavaPlugin implements Plugin<Project> {
#Override
public void apply(Project target) {
// CHANGE HERE
target.getTasks().create("javaTask", MyJavaTask.class);
}
}
Consumer script
projectRoot/consumer/build.gradle
This change removes the now unnecessary task definition
buildscript {
repositories {
maven {
url uri('../repo')
}
}
dependencies {
classpath group: 'org.joefernandez',
name: 'MyJavaPlugin',
version: '1.0-SNAPSHOT'
}
}
apply plugin: 'test-plugin'
// CHANGE HERE
This them changes step 6, to:
Execute the script that depends on the plugin: $ gradle javaTask
Plugin classes must implement the org.gradle.api.Plugin interface. Task classes typically extend the org.gradle.api.DefaultTask class. It doesn't matter which (JVM) language is used for this. The Gradle codebase contains many plugin and task classes implemented in Java.
How to build and ship a plugin is another question, again mostly unrelated to which language is used. See the Gradle User Guide and samples/customPlugin in the full Gradle distribution.

Categories