Skip to content

Commit

Permalink
Refactor gradle plugin to use Gradle best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed Aug 31, 2016
1 parent 331a771 commit 2654ce3
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 454 deletions.
19 changes: 10 additions & 9 deletions byte-buddy-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'java-gradle-plugin'

def pom = new XmlSlurper().parse(file('./pom.xml'))

group = pom.parent.groupId
version = pom.parent.version
group = pom.parent.groupId.text().toString()
version = pom.parent.version.text().toString()

sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
Expand All @@ -16,17 +18,16 @@ repositories {
dependencies {
compile gradleApi()
compile group: group, name: 'byte-buddy', version: version
compile group: 'org.eclipse.aether', name: 'aether-api', version: '1.1.0'
compile group: 'org.eclipse.aether', name: 'aether-util', version: '1.1.0'
compile group: 'org.eclipse.aether', name: 'aether-connector-basic', version: '1.1.0'
compile group: 'org.eclipse.aether', name: 'aether-transport-file', version: '1.1.0'
compile group: 'org.eclipse.aether', name: 'aether-transport-http', version: '1.1.0'
compile group: 'org.eclipse.aether', name: 'aether-transport-wagon', version: '1.1.0'
compile group: 'org.apache.maven', name: 'maven-aether-provider', version: '3.3.9'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile gradleTestKit()
testCompile 'com.google.guava:guava:19.0'
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

test {
systemProperty('byte_buddy_version', version)
}
Binary file modified byte-buddy-gradle-plugin/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Aug 30 09:29:34 CEST 2016
#Wed Aug 31 09:16:34 EEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-bin.zip

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,72 +1,78 @@
package net.bytebuddy.build.gradle;

import java.util.ArrayList;
import java.util.List;

import groovy.lang.Closure;
import org.gradle.api.GradleException;
import org.gradle.api.Project;

import java.util.ArrayList;
import java.util.List;

public class ByteBuddyExtension {

private final Project project;
private final Project project;

private List<Transformation> transformations;

private List<Transformation> transformations;
private Initialization initialization;

private Initialization initialization;
private String suffix;

private String suffix;
private boolean failOnLiveInitializer;

private boolean failOnLiveInitializer;
public ByteBuddyExtension(Project project) {
this.project = project;
transformations = new ArrayList<Transformation>();
failOnLiveInitializer = true;
}

public ByteBuddyExtension(Project project) {
this.project = project;
transformations = new ArrayList<Transformation>();
failOnLiveInitializer = true;
}
private class TransformationsConfigurer {
public Transformation transformation(Closure<?> closure) {
Transformation transformation = (Transformation) project
.configure(new Transformation(), closure);
transformations.add(transformation);
return transformation;
}
}

public Transformation transformation(Closure<?> closure) {
Transformation transformation = (Transformation) project.configure(new Transformation(project), closure);
transformations.add(transformation);
return transformation;
}
public void transformations(Closure<?> closure) {
project.configure(new TransformationsConfigurer(), closure);
}

public Initialization initialization(Closure<?> closure) {
if (initialization != null) {
throw new GradleException("Initialization is already set");
}
Initialization initialization = (Initialization) project.configure(new Initialization(project), closure);
this.initialization = initialization;
return initialization;
}
public Initialization initialization(Closure<?> closure) {
if (initialization != null) {
throw new GradleException("Initialization is already set");
}
Initialization initialization = (Initialization) project
.configure(new Initialization(), closure);
this.initialization = initialization;
return initialization;
}

public List<Transformation> getTransformations() {
return transformations;
}
public List<Transformation> getTransformations() {
return transformations;
}

public Initialization getInitialization() {
return initialization == null
? Initialization.makeDefault(project)
: initialization;
}
public Initialization getInitialization() {
return initialization == null ? Initialization.makeDefault() : initialization;
}

public void setInitialization(Initialization initialization) {
this.initialization = initialization;
}
public void setInitialization(Initialization initialization) {
this.initialization = initialization;
}

public String getSuffix() {
return suffix;
}
public String getSuffix() {
return suffix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}

public boolean isFailOnLiveInitializer() {
return failOnLiveInitializer;
}
public boolean isFailOnLiveInitializer() {
return failOnLiveInitializer;
}

public void setFailOnLiveInitializer(boolean failOnLiveInitializer) {
this.failOnLiveInitializer = failOnLiveInitializer;
}
public void setFailOnLiveInitializer(boolean failOnLiveInitializer) {
this.failOnLiveInitializer = failOnLiveInitializer;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
package net.bytebuddy.build.gradle;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.*;
import org.gradle.api.tasks.compile.AbstractCompile;

public class ByteBuddyPlugin implements Plugin<Project> {

/*
* TODO:
*
* 1. Create complex configuration (allow nesting and lists).
* 2. Find way to append task to class compilation / test-class compilation.
* 3. Integrate Aether and read (Maven?) repositories from user configuration.
* 4. Read class path / test class path.
*
* Bonus:
* 1. Read explicit dependencies from Maven POM file instead of duplication in build.gradle.
* 2. Are main and test folders of build target customizable locations?
* 3. What GradleExceptions should be thrown? Any sub types?
*/
import java.io.File;
import java.io.IOException;

public class ByteBuddyPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getExtensions().create("byteBuddy", ByteBuddyExtension.class, project);
project.getTasks().create("transform", TransformTask.ForProductionTypes.class);
project.getTasks().create("test-transform", TransformTask.ForTestTypes.class);
public void apply(final Project project) {
final ByteBuddyExtension byteBuddyExtension = project.getExtensions()
.create("byteBuddy", ByteBuddyExtension.class, project);
project.getTasks().withType(AbstractCompile.class, new Action<AbstractCompile>() {
@Override
public void execute(AbstractCompile compileTask) {
final Iterable<File> compileClasspathFiles = compileTask.getClasspath();
final File classesDir = compileTask.getDestinationDir();
compileTask.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
try {
new Transformer(project, byteBuddyExtension)
.processOutputDirectory(classesDir,
compileClasspathFiles);
} catch (IOException e) {
throw new GradleException(
"Exception in byte-buddy processing", e);
}
}
});
}
});
}
}
Loading

0 comments on commit 2654ce3

Please sign in to comment.