Skip to content

Commit

Permalink
Update CLI build and integ tests to work with shading
Browse files Browse the repository at this point in the history
This commit updates the Smithy CLI build to shade the JAR and still
get the same treatment as other JARs (like adding a license). This
required moving the shadow plugin to the main build.gradle and
selectively disabling it for everything but smithy-cli. Then, when
running the maven publish plugin, we switch between the normal JAR
and the shaded JAR output.

When running integration tests for a pre-release build, the JAR is
not yet in Maven Central, which resulted in integration tests failing.
The same problem occurred when running a classpath based command
(they added built-in dependencies on the current CLI version, but those
versions aren't yet in central). That isn't strictly necessary because
we filter out resolved JARs from provided JARs.

Integration tests for the CLI now use a specific version of Smithy,
1.26.0. The version doesn't really matter since it's ignored when
performing the actual CLI build (since the provided JARs supersede
the given version).
  • Loading branch information
mtdowling committed Dec 20, 2022
1 parent 185ba11 commit 3fab323
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 31 deletions.
16 changes: 14 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ plugins {
id "com.github.spotbugs" version "4.7.1"
id "io.codearte.nexus-staging" version "0.30.0"
id "me.champeau.jmh" version "0.6.5"
id 'com.github.johnrengelman.shadow' version "7.1.2"
}

ext {
Expand Down Expand Up @@ -115,6 +116,13 @@ subprojects {
// ==== Maven ====
apply plugin: "maven-publish"
apply plugin: "signing"
apply plugin: "com.github.johnrengelman.shadow"

// This is a little hacky, but currently needed to build a shadowed CLI JAR with the same customizations
// as other JARs.
if (project.name != "smithy-cli") {
tasks.shadowJar.enabled = false
}

publishing {
repositories {
Expand All @@ -130,8 +138,12 @@ subprojects {
}

publications {
mavenJava(MavenPublication) {
from(components["java"])
mavenJava(MavenPublication) { publication ->
if (tasks.shadowJar.enabled) {
project.shadow.component(publication)
} else {
publication.from(components["java"])
}

// Ship the source and javadoc jars.
artifact(tasks["sourcesJar"])
Expand Down
11 changes: 1 addition & 10 deletions smithy-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import java.nio.file.Paths
plugins {
id "application"
id "org.beryx.runtime" version "1.12.7"
id 'com.github.johnrengelman.shadow' version "7.1.2"
}

description = "This module implements the Smithy command line interface."
Expand Down Expand Up @@ -58,17 +57,9 @@ dependencies {

// ------ Shade Maven dependency resolvers into the JAR. -------

publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
}

shadowJar {
// Replace the normal JAR with the shaded JAR.
archiveClassifier.set('')
archiveClassifier = ''

mergeServiceFiles()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import software.amazon.smithy.utils.MapUtils;

public class MavenResolverTest {

private static final String TEST_VERSION = "1.26.0";

@Test
public void resolvesDependenciesFromMavenCentralDefault() {
IntegUtils.run("aws-model", ListUtils.of("validate", "model"), result -> {
Expand Down Expand Up @@ -46,7 +49,7 @@ public void lowerSmithyVersionsAreUpgradedToNewerVersions() {
public void lowerSmithyVersionsAreUpgradedToNewerVersionsQuiet() {
IntegUtils.run("lower-smithy-version", ListUtils.of("validate", "--quiet"), result -> {
assertThat(result.getExitCode(), equalTo(0));
assertThat(result.getOutput(), not(containsString("Replacing software.amazon.smithy:smithy-model:jar:1.0.0 with software.amazon.smithy:smithy-model:" + SmithyCli.getVersion())));
assertThat(result.getOutput(), not(containsString("Replacing software.amazon.smithy:smithy-model:jar:1.0.0")));
});
}

Expand Down Expand Up @@ -116,7 +119,7 @@ public void invalidatesCacheWhenDependencyChanges() {

ObjectNode node = Node.parse(cacheContents).expectObjectNode();
String location = node.expectStringMember("software.amazon.smithy:smithy-aws-traits:"
+ SmithyCli.getVersion()).getValue();
+ TEST_VERSION).getValue();

// Set the lastModified of the JAR to the current time, which is > than the time of the config file,
// so the cache is invalided.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"version": "1.0",
"maven": {
"dependencies": ["software.amazon.smithy:smithy-aws-traits:${SMITHY_VERSION}"]
// Normally, this could refer to SMITHY_VERSION, but that doesn't work for pre-release builds.
"dependencies": ["software.amazon.smithy:smithy-aws-traits:1.26.0"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
}
],
"dependencies": [
"software.amazon.smithy:smithy-aws-iam-traits:${SMITHY_VERSION}"
// Normally, this could refer to SMITHY_VERSION, but that doesn't work for pre-release builds.
"software.amazon.smithy:smithy-aws-iam-traits:1.26.0"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@
import software.amazon.smithy.cli.dependencies.FileCacheResolver;
import software.amazon.smithy.cli.dependencies.FilterCliVersionResolver;
import software.amazon.smithy.cli.dependencies.ResolvedArtifact;
import software.amazon.smithy.utils.SetUtils;

abstract class ClasspathCommand extends SimpleCommand {

private static final Set<String> PROVIDED_SMITHY_DEPENDENCIES = SetUtils.of(
"software.amazon.smithy:smithy-cli:",
"software.amazon.smithy:smithy-model:",
"software.amazon.smithy:smithy-build:",
"software.amazon.smithy:smithy-utils:");
private static final Logger LOGGER = Logger.getLogger(ClasspathCommand.class.getName());
private static final MavenRepository CENTRAL = MavenRepository.builder()
.url("https://repo.maven.apache.org/maven2")
Expand Down Expand Up @@ -151,7 +145,6 @@ private List<Path> resolveDependencies(
long lastModified = smithyBuildConfig.getLastModifiedInMillis();
DependencyResolver delegate = new FilterCliVersionResolver(SmithyCli.getVersion(), baseResolver);
DependencyResolver resolver = new FileCacheResolver(getCacheFile(buildOptions), lastModified, delegate);
addDefaultConfiguration(resolver);
addConfiguredMavenRepos(smithyBuildConfig, resolver);
maven.getDependencies().forEach(resolver::addDependency);
List<ResolvedArtifact> artifacts = resolver.resolve();
Expand All @@ -165,13 +158,6 @@ private List<Path> resolveDependencies(
return result;
}

private static void addDefaultConfiguration(DependencyResolver resolver) {
String version = SmithyCli.getVersion();
for (String provided : PROVIDED_SMITHY_DEPENDENCIES) {
resolver.addDependency(provided + version);
}
}

private static void addConfiguredMavenRepos(SmithyBuildConfig config, DependencyResolver resolver) {
// Environment variables take precedence over config files.
String envRepos = EnvironmentVariable.SMITHY_MAVEN_REPOS.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ int runWithClassLoader(SmithyBuildConfig config, Arguments arguments, Env env, L

private void resolve(DependencyResolver resolver) {
resolver.addRepository(MavenRepository.builder().url("https://repo.maven.apache.org/maven2").build());
resolver.addDependency("software.amazon.smithy:smithy-model:" + SmithyCli.getVersion());
// Use a version that we know exists in Maven Central. The version doesn't matter because it will be ignored
// when the result is filtered. This ensures that things like pre-release builds that aren't in Central
// work correctly.
resolver.addDependency("software.amazon.smithy:smithy-model:1.26.0");
resolver.resolve();
}
}

0 comments on commit 3fab323

Please sign in to comment.