Currently I have this task in my build.gradle file
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
classifier = 'sources'
}
I want to have this task created by a binary plugin written in Java.
My question is how do I write this in Java using org.gradle?
Chris_Dore
(Chris Doré)
2
The following plugin class should give you some ideas to go from (I did not execute/test this code):
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.bundling.Jar;
public class SourcesJarPlugin implements Plugin<Project>
{
@Override
public void apply( final Project project )
{
project.getPluginManager().withPlugin( "java", appliedPlugin -> {
project.getTasks().register( "sourcesJar", Jar.class, jarTask -> {
SourceSetContainer sourceSets = project.getExtensions().getByType( SourceSetContainer.class );
jarTask.from( sourceSets.getByName( "main" ).getAllJava() );
jarTask.getArchiveClassifier().set( "sources" );
} );
} );
}
}
In case you are unaware, modern Gradle’s java plugin has the ability to create and publish a sources jar by calling withSourcesJar()
in the java
extension. https://docs.gradle.org/current/userguide/java_plugin.html#sec:java-extension