Skip to content

Commit

Permalink
BaselineJavaVersions handles the lazy 'nebula.maven-publish' plugin (#…
Browse files Browse the repository at this point in the history
…1986)

BaselineJavaVersions handles the lazy 'nebula.maven-publish' plugin
  • Loading branch information
carterkozak authored Nov 23, 2021
1 parent 7dd383e commit 3b92601
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 20 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1986.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: BaselineJavaVersions handles the lazy 'nebula.maven-publish' plugin
links:
- https://github.com/palantir/gradle-baseline/pull/1986
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import org.gradle.api.publish.ivy.IvyPublication;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.util.GradleVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class BaselineJavaVersions implements Plugin<Project> {

private static final Logger log = LoggerFactory.getLogger(BaselineJavaVersions.class);
public static final String EXTENSION_NAME = "javaVersions";

public static final GradleVersion MIN_GRADLE_VERSION = GradleVersion.version("7.0");
Expand Down Expand Up @@ -62,31 +65,36 @@ public void apply(Project project) {
}

private static boolean isLibrary(Project project) {
if (project.getPluginManager().hasPlugin("nebula.maven-publish")) {
// 'nebula.maven-publish' creates publications lazily which causes inconsistencies based
// on ordering.
log.debug(
"Project '{}' is considered a library because the 'nebula.maven-publish' plugin is applied",
project.getDisplayName());
return true;
}
PublishingExtension publishing = project.getExtensions().findByType(PublishingExtension.class);
if (publishing == null) {
project.getLogger()
.debug(
"Project '{}' is considered a distribution, not a library, because "
+ "it doesn't define any publishing extensions",
project.getDisplayName());
log.debug(
"Project '{}' is considered a distribution, not a library, because "
+ "it doesn't define any publishing extensions",
project.getDisplayName());
return false;
}
ImmutableList<String> jarPublications = publishing.getPublications().stream()
.filter(pub -> isLibraryPublication(project, pub))
.map(Named::getName)
.collect(ImmutableList.toImmutableList());
if (jarPublications.isEmpty()) {
project.getLogger()
.debug(
"Project '{}' is considered a distribution because it does not publish jars",
project.getDisplayName());
log.debug(
"Project '{}' is considered a distribution because it does not publish jars",
project.getDisplayName());
return false;
}
project.getLogger()
.debug(
"Project '{}' is considered a library because it publishes jars: {}",
project.getDisplayName(),
jarPublications);
log.debug(
"Project '{}' is considered a library because it publishes jars: {}",
project.getDisplayName(),
jarPublications);
return true;
}

Expand All @@ -100,12 +108,11 @@ private static boolean isLibraryPublication(Project project, Publication publica
return ivyPublication.getArtifacts().stream().anyMatch(artifact -> "jar".equals(artifact.getExtension()));
}
// Default to true for unknown publication types to avoid setting higher jvm targets than necessary
project.getLogger()
.warn(
"Unknown publication '{}' of type '{}'. Assuming project {} is a library",
publication,
publication.getClass().getName(),
project.getName());
log.warn(
"Unknown publication '{}' of type '{}'. Assuming project {} is a library",
publication,
publication.getClass().getName(),
project.getName());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ import spock.lang.Unroll
class BaselineJavaVersionIntegrationTest extends IntegrationSpec {
private static final int JAVA_8_BYTECODE = 52
private static final int JAVA_11_BYTECODE = 55
private static final int JAVA_17_BYTECODE = 61

def standardBuildFile = '''
buildscript {
repositories { mavenCentral() }
dependencies {
classpath 'com.netflix.nebula:nebula-publishing-plugin:17.0.0'
}
}
plugins {
id 'java-library'
id 'application'
Expand Down Expand Up @@ -80,6 +87,39 @@ class BaselineJavaVersionIntegrationTest extends IntegrationSpec {
runTasksWithFailure('compileJava')
}

def 'distribution target is used when no artifacts are published'() {
when:
buildFile << '''
javaVersions {
libraryTarget = 11
distributionTarget = 17
}
'''.stripIndent(true)
file('src/main/java/Main.java') << java11CompatibleCode
File compiledClass = new File(projectDir, "build/classes/java/main/Main.class")

then:
runTasksSuccessfully('compileJava')
getBytecodeVersion(compiledClass) == JAVA_17_BYTECODE
}

def 'library target is used when nebula maven publishing plugin is applied'() {
when:
buildFile << '''
apply plugin: 'nebula.maven-publish'
javaVersions {
libraryTarget = 11
distributionTarget = 17
}
'''.stripIndent(true)
file('src/main/java/Main.java') << java11CompatibleCode
File compiledClass = new File(projectDir, "build/classes/java/main/Main.class")

then:
runTasksSuccessfully('compileJava')
getBytecodeVersion(compiledClass) == JAVA_11_BYTECODE
}

def 'java 11 compilation succeeds targeting java 11'() {
when:
buildFile << '''
Expand Down

0 comments on commit 3b92601

Please sign in to comment.