Skip to content

Commit

Permalink
fix: replace deprecated archivePath with archiveFile in a backwards c…
Browse files Browse the repository at this point in the history
…ompatible way (#451)
  • Loading branch information
TWiStErRob authored Jan 25, 2023
1 parent 5d39c93 commit ffd72d7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.tools.gradle.appengine.core.DeployExtension;
import com.google.cloud.tools.gradle.appengine.core.DeployTask;
import com.google.cloud.tools.gradle.appengine.core.ToolsExtension;
import com.google.cloud.tools.gradle.appengine.util.GradleCompatibility;
import java.io.File;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
Expand Down Expand Up @@ -89,11 +90,11 @@ private void configureExtensions() {
// we can only set the default location of "archive" after project evaluation (callback)
if (stageExtension.getArtifact() == null) {
if (project.getPlugins().hasPlugin(WarPlugin.class)) {
War war = (War) project.getProperties().get("war");
stageExtension.setArtifact(war.getArchivePath());
War war = (War) project.getProperties().get(WarPlugin.WAR_TASK_NAME);
stageExtension.setArtifact(GradleCompatibility.getArchiveFile(war));
} else if (project.getPlugins().hasPlugin(JavaPlugin.class)) {
Jar jar = (Jar) project.getProperties().get("jar");
stageExtension.setArtifact(jar.getArchivePath());
Jar jar = (Jar) project.getProperties().get(JavaPlugin.JAR_TASK_NAME);
stageExtension.setArtifact(GradleCompatibility.getArchiveFile(jar));
} else {
throw new GradleException("Could not find JAR or WAR configuration");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.WarPlugin;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.bundling.War;
Expand Down Expand Up @@ -89,8 +91,10 @@ private void createSourceContextTask() {
genRepoInfoFile.setGcloud(cloudSdkOperations.getGcloud());
});
});
configureArchiveTask(project.getTasks().withType(War.class).findByName("war"));
configureArchiveTask(project.getTasks().withType(Jar.class).findByName("jar"));
configureArchiveTask(
project.getTasks().withType(War.class).findByName(WarPlugin.WAR_TASK_NAME));
configureArchiveTask(
project.getTasks().withType(Jar.class).findByName(JavaPlugin.JAR_TASK_NAME));
}

// inject source-context into the META-INF directory of a jar or war
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.tools.gradle.appengine.core.DeployTargetResolver;
import com.google.cloud.tools.gradle.appengine.core.DeployTask;
import com.google.cloud.tools.gradle.appengine.core.ToolsExtension;
import com.google.cloud.tools.gradle.appengine.util.GradleCompatibility;
import com.google.common.base.Strings;
import java.io.File;
import org.gradle.api.GradleException;
Expand Down Expand Up @@ -147,10 +148,10 @@ private void createExplodedWarTask() {
explodeWar.setDescription("Explode a war into a directory");

project.afterEvaluate(
project ->
explodeWar.setWarFile(
((War) project.getTasks().getByPath(WarPlugin.WAR_TASK_NAME))
.getArchivePath()));
project -> {
War war = (War) project.getTasks().getByPath(WarPlugin.WAR_TASK_NAME);
explodeWar.setWarFile(GradleCompatibility.getArchiveFile(war));
});
});
project.getTasks().getByName(BasePlugin.ASSEMBLE_TASK_NAME).dependsOn(EXPLODE_WAR_TASK_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.google.cloud.tools.gradle.appengine.util;

import java.io.File;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.util.GradleVersion;

/** Utility class for Gradle compatibility related functions. As an end user, do not use. */
public class GradleCompatibility {

private GradleCompatibility() {
// Prevent instantiation and extension.
}

/**
* Compatibility method for getting the archive location.
*
* @param task the task whose archive location we're interested in.
* @return the archive location as a {@link File} for compatibility with older Gradle versions.
*/
@SuppressWarnings("deprecation")
public static File getArchiveFile(AbstractArchiveTask task) {
// getArchiveFile and getArchivePath history:
// - Gradle 5.1.0-M1 added `getArchiveFile`
// - Gradle 5.1.0-M1 deprecated `getArchivePath`
// - Gradle 6.0.0-RC1 added nagging to `getArchivePath`
// - Gradle 6.0.0-RC1 removed nagging of `getArchivePath`
// - Gradle 7.1.0-RC1 re-enabled nagging of `getArchivePath` for Gradle 8
// - Gradle 7.1.0-RC1 removed nagging of `getArchivePath`
// - Gradle 8.0-M2 removed `getArchivePath`
// - Gradle 8.0-M2 re-added `getArchivePath` with nagging for Gradle 9
// For full history with references see:
// https://github.com/GoogleCloudPlatform/app-gradle-plugin/pull/451

if (GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("5.1")) >= 0) {
return task.getArchiveFile().get().getAsFile();
} else {
// Note: in case this fails to compile, because Gradle have succeeded with the removal,
// this needs to be replaced with a reflective call to getArchivePath()
// or the minimum Gradle version for GCP Gradle Plugin bumped up to 5.1+.
return task.getArchivePath();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

public class NullSafe {

private NullSafe() {
// Prevent instantiation and extension.
}

public static <S, R> R convert(S source, Function<S, R> converter) {
return (source == null) ? null : converter.apply(source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.io.IOException;
import java.util.List;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.WarPlugin;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.bundling.War;
import org.gradle.testkit.runner.BuildResult;
Expand Down Expand Up @@ -187,7 +189,8 @@ public void testDefaultConfiguration() throws IOException {
assertEquals(
testProjectDir.getRoot().toPath().toRealPath().resolve("src/main/appengine"),
deployExt.getAppEngineDirectory().toPath());
assertEquals((((War) p.getProperties().get("war")).getArchivePath()), stageExt.getArtifact());
War war = (War) p.getProperties().get(WarPlugin.WAR_TASK_NAME);
assertEquals(war.getArchiveFile().get().getAsFile(), stageExt.getArtifact());
assertFalse(new File(testProjectDir.getRoot(), "src/main/docker").exists());

assertEquals("test-project", deployExt.getProjectId());
Expand All @@ -203,7 +206,8 @@ public void testDefaultConfigurationAlternative() {
StageAppYamlExtension stageExt = ext.getStage();

assertTrue(new File(testProjectDir.getRoot(), "src/main/docker").exists());
assertEquals((((Jar) p.getProperties().get("jar")).getArchivePath()), stageExt.getArtifact());
Jar jar = (Jar) p.getProperties().get(JavaPlugin.JAR_TASK_NAME);
assertEquals(jar.getArchiveFile().get().getAsFile(), stageExt.getArtifact());
}

@Test
Expand Down

0 comments on commit ffd72d7

Please sign in to comment.