Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaselineJavaVersions handles the lazy 'nebula.maven-publish' plugin #1986

Merged
merged 2 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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