Skip to content

Commit

Permalink
Use 'java-library' plugin component for publishing instead of War plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jan 28, 2020
1 parent 7764bb2 commit 4f4495a
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
package org.jenkinsci.gradle.plugins.jpi

import org.gradle.api.Project
import org.gradle.api.plugins.WarPlugin
import org.gradle.api.plugins.WarPluginConvention
import org.gradle.api.tasks.bundling.War

/**
* @author Kohsuke Kawaguchi
Expand All @@ -27,15 +24,13 @@ class JpiHplManifest extends JpiManifest {
JpiHplManifest(Project project) {
super(project)

def conv = project.extensions.getByType(JpiExtension)
War war = project.tasks.getByName(WarPlugin.WAR_TASK_NAME) as War
def jpiExtension = project.extensions.getByType(JpiExtension)

// src/main/webApp
def warconv = project.convention.getPlugin(WarPluginConvention)
mainAttributes.putValue('Resource-Path', warconv.webAppDir.absolutePath)
mainAttributes.putValue('Resource-Path', project.file(JpiPlugin.WEB_APP_DIR).absolutePath)

// add resource directories directly so that we can pick up the source, then add all the jars and class path
Set<File> libraries = conv.mainSourceTree().resources.srcDirs + war.classpath.files
Set<File> libraries = jpiExtension.mainSourceTree().output.files
mainAttributes.putValue('Libraries', libraries.findAll { it.exists() }.join(','))
}
}
106 changes: 31 additions & 75 deletions src/main/groovy/org/jenkinsci/gradle/plugins/jpi/JpiPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ package org.jenkinsci.gradle.plugins.jpi
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.plugins.JavaLibraryPlugin
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.plugins.WarPlugin
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.plugins.GroovyPlugin
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
Expand All @@ -40,12 +40,11 @@ import org.gradle.api.tasks.bundling.War
import org.gradle.api.tasks.compile.GroovyCompile
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.testing.Test
import org.gradle.util.GradleVersion

import static org.gradle.api.logging.LogLevel.INFO
import static org.gradle.api.plugins.JavaPlugin.API_CONFIGURATION_NAME
import static org.gradle.api.plugins.JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME
import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME
import static org.gradle.api.plugins.WarPlugin.PROVIDED_COMPILE_CONFIGURATION_NAME
import static org.gradle.api.plugins.WarPlugin.PROVIDED_RUNTIME_CONFIGURATION_NAME
import static org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME
import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME
import static org.jenkinsci.gradle.plugins.jpi.JpiManifest.attributesToMap
Expand Down Expand Up @@ -91,13 +90,11 @@ class JpiPlugin implements Plugin<Project> {
public static final String JENKINS_SERVER_DEPENDENCY_CONFIGURATION_NAME = 'jenkinsServer'

public static final String JPI_TASK_NAME = 'jpi'
public static final String SOURCES_JAR_TASK_NAME = 'sourcesJar'
public static final String JAVADOC_JAR_TASK_NAME = 'javadocJar'
public static final String LICENSE_TASK_NAME = 'generateLicenseInfo'
public static final String WEB_APP_DIR = "src/main/webapp"

void apply(final Project gradleProject) {
gradleProject.plugins.apply(JavaPlugin)
gradleProject.plugins.apply(WarPlugin)
gradleProject.plugins.apply(JavaLibraryPlugin)
gradleProject.plugins.apply(GroovyPlugin)

def ext = gradleProject.extensions.create('jenkinsPlugin', JpiExtension, gradleProject)
Expand All @@ -114,29 +111,11 @@ class JpiPlugin implements Plugin<Project> {
}

configureLocalizer(gradleProject)
configureLicenseInfo(gradleProject)
configureInjectedTest(gradleProject)

gradleProject.tasks.register(SOURCES_JAR_TASK_NAME, Jar) {
it.dependsOn('classes')
def classifier = 'sources'
if (GradleVersion.current() >= GradleVersion.version('5.1')) {
it.archiveClassifier.set(classifier)
} else {
it.classifier = classifier
}
it.from gradleProject.sourceSets.main.allSource
}
gradleProject.tasks.register(JAVADOC_JAR_TASK_NAME, Jar) {
it.dependsOn('javadoc')
def classifier = 'javadoc'
if (GradleVersion.current() >= GradleVersion.version('5.1')) {
it.archiveClassifier.set(classifier)
} else {
it.classifier = classifier
}
it.from gradleProject.javadoc.destinationDir
}
JavaPluginExtension javaPluginExtension = gradleProject.extensions.getByType(JavaPluginExtension)
javaPluginExtension.withSourcesJar()
javaPluginExtension.withJavadocJar()

if (!gradleProject.logger.isEnabled(INFO)) {
gradleProject.tasks.withType(JavaCompile).configureEach {
Expand All @@ -153,8 +132,9 @@ class JpiPlugin implements Plugin<Project> {

configureRepositories(gradleProject)
configureConfigurations(gradleProject)
configureManifest(gradleProject)
configureJpi(gradleProject)
configureManifest(gradleProject)
configureLicenseInfo(gradleProject)
configureTestDependencies(gradleProject)
configurePublishing(gradleProject)
configureTestHpl(gradleProject)
Expand All @@ -175,13 +155,13 @@ class JpiPlugin implements Plugin<Project> {

private static configureManifest(Project project) {
JavaPluginConvention javaPluginConvention = project.convention.getPlugin(JavaPluginConvention)
TaskProvider<War> warProvider = project.tasks.named(WarPlugin.WAR_TASK_NAME) as TaskProvider<War>
TaskProvider<War> jpiProvider = project.tasks.named(JPI_TASK_NAME) as TaskProvider<War>
TaskProvider<Jar> jarProvider = project.tasks.named(JavaPlugin.JAR_TASK_NAME) as TaskProvider<Jar>

def configureManifest = project.tasks.register('configureManifest') {
it.doLast {
Map<String, ?> attributes = attributesToMap(new JpiManifest(project).mainAttributes)
warProvider.configure {
jpiProvider.configure {
it.manifest.attributes(attributes)
it.inputs.property('manifest', attributes)
}
Expand All @@ -194,36 +174,23 @@ class JpiPlugin implements Plugin<Project> {
it.dependsOn(javaPluginConvention.sourceSets.getByName(MAIN_SOURCE_SET_NAME).output)
}

warProvider.configure { it.dependsOn(configureManifest) }
jpiProvider.configure { it.dependsOn(configureManifest) }
jarProvider.configure { it.dependsOn(configureManifest) }
}

private static configureJpi(Project project) {
JpiExtension jpiExtension = project.extensions.getByType(JpiExtension)

def jar = project.tasks.named(JavaPlugin.JAR_TASK_NAME)
def war = project.tasks.named(WarPlugin.WAR_TASK_NAME)
project.afterEvaluate {
war.configure {
it.description = 'Generates the JPI package'
def fileName = "${jpiExtension.shortName}.${jpiExtension.fileExtension}"
def extension = jpiExtension.fileExtension
if (GradleVersion.current() >= GradleVersion.version('5.1')) {
it.archiveFileName.set(fileName)
it.archiveExtension.set(extension)
} else {
it.archiveName = fileName
it.extension = extension
}
it.classpath -= project.sourceSets.main.output
it.classpath(jar)
}
}

project.tasks.register(JPI_TASK_NAME) {
it.dependsOn(war)
project.tasks.register(JPI_TASK_NAME, War) {
it.description = 'Generates the JPI package'
it.group = BasePlugin.BUILD_GROUP
def fileName = "${jpiExtension.shortName}.${jpiExtension.fileExtension}"
def extension = jpiExtension.fileExtension
it.archiveFileName.set(fileName)
it.archiveExtension.set(extension)
it.classpath(jar, project.configurations.named(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME))
it.from(WEB_APP_DIR)
}
}

Expand Down Expand Up @@ -280,16 +247,14 @@ class JpiPlugin implements Plugin<Project> {
it.group = BasePlugin.BUILD_GROUP
it.outputDirectory = new File(project.buildDir, 'licenses')
it.configurations = [
project.configurations[javaConvention.sourceSets.main.compileConfigurationName],
project.configurations[javaConvention.sourceSets.main.runtimeConfigurationName],
project.configurations[javaConvention.sourceSets.main.runtimeClasspathConfigurationName]
]
it.providedConfigurations = [
project.configurations[PROVIDED_COMPILE_CONFIGURATION_NAME],
project.configurations[PROVIDED_RUNTIME_CONFIGURATION_NAME],
project.configurations[COMPILE_ONLY_CONFIGURATION_NAME]
]
}

project.tasks.named(WarPlugin.WAR_TASK_NAME).configure {
project.tasks.named(JPI_TASK_NAME).configure {
it.webInf.from(licenseTask.get().outputDirectory)
it.dependsOn(licenseTask)
}
Expand Down Expand Up @@ -354,9 +319,9 @@ class JpiPlugin implements Plugin<Project> {
test.visible = false
test.description = 'Jenkins plugin test dependencies.'

project.configurations.getByName(PROVIDED_COMPILE_CONFIGURATION_NAME).extendsFrom(core)
project.configurations.getByName(PROVIDED_COMPILE_CONFIGURATION_NAME).extendsFrom(plugins)
project.configurations.getByName(PROVIDED_COMPILE_CONFIGURATION_NAME).extendsFrom(optionalPlugins)
project.configurations.getByName(COMPILE_ONLY_CONFIGURATION_NAME).extendsFrom(core)
project.configurations.getByName(COMPILE_ONLY_CONFIGURATION_NAME).extendsFrom(plugins)
project.configurations.getByName(COMPILE_ONLY_CONFIGURATION_NAME).extendsFrom(optionalPlugins)
project.configurations.getByName(TEST_IMPLEMENTATION_CONFIGURATION_NAME).extendsFrom(test)

Configuration warDependencies = project.configurations.create(WAR_DEPENDENCY_CONFIGURATION_NAME)
Expand All @@ -380,21 +345,12 @@ class JpiPlugin implements Plugin<Project> {
// delay configuration until all settings are available (groupId, shortName, ...)
project.afterEvaluate {
if (jpiExtension.configurePublishing) {
Task jar = project.tasks.getByName(JavaPlugin.JAR_TASK_NAME)
Task sourcesJar = project.tasks.getByName(SOURCES_JAR_TASK_NAME)
Task javadocJar = project.tasks.getByName(JAVADOC_JAR_TASK_NAME)

project.plugins.apply(MavenPublishPlugin)
PublishingExtension publishingExtension = project.extensions.getByType(PublishingExtension)
publishingExtension.publications {
mavenJpi(MavenPublication) {
artifactId jpiExtension.shortName

from(project.components.web)

artifact jar
artifact sourcesJar
artifact javadocJar
from(project.components.java)

new JpiPomCustomizer(project).customizePom(pom)
}
Expand Down Expand Up @@ -444,12 +400,12 @@ class JpiPlugin implements Plugin<Project> {
resolvePluginDependencies(
project,
PLUGINS_DEPENDENCY_CONFIGURATION_NAME,
PROVIDED_COMPILE_CONFIGURATION_NAME
API_CONFIGURATION_NAME
)
resolvePluginDependencies(
project,
OPTIONAL_PLUGINS_DEPENDENCY_CONFIGURATION_NAME,
PROVIDED_COMPILE_CONFIGURATION_NAME
API_CONFIGURATION_NAME
)
resolvePluginDependencies(
project,
Expand All @@ -472,7 +428,7 @@ class JpiPlugin implements Plugin<Project> {
}

private static Dependency toDependency(Project project, ResolvedArtifact it, String from) {
project.dependencies.create("${it.moduleVersion.id}@jar") { Dependency d ->
project.dependencies.create("${it.moduleVersion.id}") { Dependency d ->
d.because "JpiPlugin added jar for compilation support (plugin present on $from)"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package org.jenkinsci.gradle.plugins.jpi
import groovy.transform.CompileStatic
import org.gradle.api.Project
import org.gradle.api.XmlProvider
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.DependencySet
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.Property
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPomDeveloper
Expand All @@ -20,8 +17,6 @@ import org.gradle.api.publish.maven.MavenPomScm
import static org.gradle.api.artifacts.ArtifactRepositoryContainer.DEFAULT_MAVEN_CENTRAL_REPO_NAME
import static org.gradle.api.artifacts.ArtifactRepositoryContainer.DEFAULT_MAVEN_LOCAL_REPO_NAME
import static org.jenkinsci.gradle.plugins.jpi.JpiPlugin.CORE_DEPENDENCY_CONFIGURATION_NAME
import static org.jenkinsci.gradle.plugins.jpi.JpiPlugin.OPTIONAL_PLUGINS_DEPENDENCY_CONFIGURATION_NAME
import static org.jenkinsci.gradle.plugins.jpi.JpiPlugin.PLUGINS_DEPENDENCY_CONFIGURATION_NAME

/**
* Adds metadata to the JPI's POM.
Expand Down Expand Up @@ -103,67 +98,15 @@ class JpiPomCustomizer {
if (repositories) {
pom.appendNode('repositories', repositories.collect { makeRepositoryNode(it) })
}
fixDependencies(pom)
// TODO Can this be removed? 'provided' dependencies are not of for poms that are metadata (they are only for local project poms in Maven)
addProvidedDependencies(pom)
}

private void fixDependencies(Node pom) {
DependencySet pluginDependencies = project.configurations.
getByName(PLUGINS_DEPENDENCY_CONFIGURATION_NAME).dependencies
DependencySet optionalPluginDependencies = project.configurations.
getByName(OPTIONAL_PLUGINS_DEPENDENCY_CONFIGURATION_NAME).dependencies
private void addProvidedDependencies(Node pom) {
DependencySet coreDependencies = project.configurations.
getByName(CORE_DEPENDENCY_CONFIGURATION_NAME).dependencies
DependencySet compileDependencies = project.configurations.
getByName(JavaPlugin.COMPILE_CONFIGURATION_NAME).dependencies
Set<Dependency> allPluginDependencies = pluginDependencies + optionalPluginDependencies

Node dependenciesNode = pom.dependencies[0] as Node
dependenciesNode = dependenciesNode ?: pom.appendNode('dependencies')
(compileDependencies - coreDependencies + pluginDependencies + optionalPluginDependencies).each {
ModuleVersionIdentifier mvid = ResolvedDependencySelector.selectedModuleVersion(
project,
'compile',
it.group,
it.name)
Node dependency = dependenciesNode.appendNode('dependency')
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', mvid.version ?: it.version)
}
dependenciesNode.each { Node dependency ->
String groupId = dependency.groupId.text()
String artifactId = dependency.artifactId.text()
Node scope = dependency.scope[0] as Node
Node exclusions = dependency.exclusions[0] as Node

// add the optional element for all optional plugin dependencies
if (optionalPluginDependencies.any { it.group == groupId && it.name == artifactId }) {
dependency.appendNode('optional', true)
}

// remove the scope for all plugin and compile dependencies
if (scope && (allPluginDependencies + compileDependencies).any {
it.group == groupId && it.name == artifactId
}) {
dependency.remove(scope)
}

// remove exclusions from all plugin dependencies
if (exclusions && allPluginDependencies.any { it.group == groupId && it.name == artifactId }) {
dependency.remove(exclusions)
}

compileDependencies.withType(ModuleDependency).find {
it.group == groupId && it.name == artifactId && it.excludeRules
}.each {
exclusions = dependency.appendNode('exclusions')
it.excludeRules.each {
Node exclusion = exclusions.appendNode('exclusion')
exclusion.appendNode('groupId', it.group)
exclusion.appendNode('artifactId', it.module)
}
}
}

coreDependencies.each {
Node dependency = dependenciesNode.appendNode('dependency')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class JpiHplManifestSpec extends Specification {
apply plugin: 'jpi'
}
def libraries = [
new File(project.projectDir, 'src/main/resources'),
new File(project.buildDir, 'classes/java/main'),
new File(project.buildDir, 'resources/main'),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ class JpiIntegrationSpec extends IntegrationSpec {
where:
task | dependency | outcome
'jar' | ':configureManifest' | TaskOutcome.SUCCESS
'war' | ':configureManifest' | TaskOutcome.SUCCESS
'jpi' | ':configureManifest' | TaskOutcome.SUCCESS
'processTestResources' | ':resolveTestDependencies' | TaskOutcome.NO_SOURCE
'jpi' | ':war' | TaskOutcome.SUCCESS
'compileTestJava' | ':insertTest' | TaskOutcome.SKIPPED
'testClasses' | ':generate-test-hpl' | TaskOutcome.SUCCESS
'compileJava' | ':localizer' | TaskOutcome.SUCCESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ class JpiPluginSpec extends Specification {
(project as ProjectInternal).evaluate()

then:
Task warTask = project.tasks[WarPlugin.WAR_TASK_NAME]
warTask != null
warTask.description != null
warTask.group == BasePlugin.BUILD_GROUP

Task jpiTask = project.tasks[JpiPlugin.JPI_TASK_NAME]
jpiTask != null
jpiTask.description != null
Expand Down
Loading

0 comments on commit 4f4495a

Please sign in to comment.