Skip to content

Commit

Permalink
feat: get transitive dependencies by group id instead of limiting to …
Browse files Browse the repository at this point in the history
…artifact id

Motivation:

Instead of having to remove transitive dependencies from new modules
by (group id, artifact id) manually (e.g.: Gatling gRPC), it is simpler
to get the dependencies of all the submodules of the known group ids
instead.
  • Loading branch information
notdryft committed Aug 16, 2023
1 parent a4d9a3d commit e924e47
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
49 changes: 28 additions & 21 deletions src/main/java/io/gatling/mojo/EnterprisePackageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ public class EnterprisePackageMojo extends AbstractEnterpriseMojo {
@Parameter(property = "gatling.excludes")
private String[] excludes;

private Set<Artifact> nonGatlingDependencies(Artifact artifact) {
if (artifact == null) {
return Collections.emptySet();
}

return resolveTransitively(artifact).stream()
.filter(art -> !GATLING_GROUP_IDS.contains(art.getGroupId()))
private Set<Artifact> nonGatlingDependencies(List<Artifact> artifacts) {
return artifacts.stream()
.flatMap(
artifact ->
resolveTransitively(artifact).stream()
.filter(art -> !GATLING_GROUP_IDS.contains(art.getGroupId())))
.collect(Collectors.toSet());
}

Expand All @@ -91,24 +90,28 @@ public void execute() throws MojoExecutionException, MojoFailureException {

Set<Artifact> allDeps = mavenProject.getArtifacts();

Artifact gatlingApp =
MojoUtils.findByGroupIdAndArtifactId(allDeps, GATLING_GROUP_ID, GATLING_MODULE_APP);
Artifact gatlingChartsHighcharts =
MojoUtils.findByGroupIdAndArtifactId(
allDeps, GATLING_HIGHCHARTS_GROUP_ID, GATLING_MODULE_HIGHCHARTS);
Artifact frontlineProbe =
MojoUtils.findByGroupIdAndArtifactId(
allDeps, GATLING_FRONTLINE_GROUP_ID, GATLING_FRONTLINE_MODULE_PROBE);
List<Artifact> depsWithFrontLineGroupId =
MojoUtils.findByGroupId(allDeps, GATLING_FRONTLINE_GROUP_ID);
if (!depsWithFrontLineGroupId.isEmpty()) {
throw new MojoExecutionException(
"Found a dependency with group id "
+ GATLING_FRONTLINE_GROUP_ID
+ " in projects dependencies");
}

if (gatlingApp == null) {
List<Artifact> depsWithGatlingGroupId = MojoUtils.findByGroupId(allDeps, GATLING_GROUP_ID);
List<Artifact> depsWithGatlingHighchartsGroupId =
MojoUtils.findByGroupId(allDeps, GATLING_HIGHCHARTS_GROUP_ID);
if (depsWithGatlingGroupId.isEmpty()) {
throw new MojoExecutionException(
"Couldn't find io.gatling:gatling-app in project dependencies");
"Couldn't find any dependencies with group id "
+ GATLING_GROUP_ID
+ " in project dependencies");
}

Set<Artifact> gatlingDependencies = new HashSet<>();
gatlingDependencies.addAll(nonGatlingDependencies(gatlingApp));
gatlingDependencies.addAll(nonGatlingDependencies(gatlingChartsHighcharts));
gatlingDependencies.addAll(nonGatlingDependencies(frontlineProbe));
gatlingDependencies.addAll(nonGatlingDependencies(depsWithGatlingGroupId));
gatlingDependencies.addAll(nonGatlingDependencies(depsWithGatlingHighchartsGroupId));

Set<Artifact> filteredDeps =
allDeps.stream()
Expand Down Expand Up @@ -207,13 +210,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// generate fake manifest
File manifest = new File(metaInfDir, "MANIFEST.MF");

String gatlingVersion =
MojoUtils.findByGroupIdAndArtifactId(allDeps, GATLING_GROUP_ID, GATLING_MODULE_APP)
.getVersion();

try (FileWriter fw = new FileWriter(manifest)) {
fw.write("Manifest-Version: 1.0\n");
fw.write("Implementation-Title: " + mavenProject.getArtifactId() + "\n");
fw.write("Implementation-Version: " + mavenProject.getVersion() + "\n");
fw.write("Implementation-Vendor: " + mavenProject.getGroupId() + "\n");
fw.write("Specification-Vendor: GatlingCorp\n");
fw.write("Gatling-Version: " + gatlingApp.getVersion() + "\n");
fw.write("Gatling-Version: " + gatlingVersion + "\n");
fw.write("Gatling-Packager: maven" + "\n");
} catch (IOException e) {
throw new MojoExecutionException("Failed to generate manifest", e);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/gatling/mojo/MojoConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ private MojoConstants() {}
static final String GATLING_GROUP_ID = "io.gatling";
static final String GATLING_MODULE_APP = "gatling-app";
static final String GATLING_HIGHCHARTS_GROUP_ID = "io.gatling.highcharts";
static final String GATLING_MODULE_HIGHCHARTS = "gatling-charts-highcharts";
static final String GATLING_FRONTLINE_GROUP_ID = "io.gatling.frontline";
static final String GATLING_FRONTLINE_MODULE_PROBE = "frontline-probe";
static final Set<String> GATLING_GROUP_IDS;

static {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/gatling/mojo/MojoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;

public final class MojoUtils {
Expand Down Expand Up @@ -129,4 +130,10 @@ static Artifact findByGroupIdAndArtifactId(
}
return null;
}

static List<Artifact> findByGroupId(Set<Artifact> artifacts, String groupId) {
return artifacts.stream()
.filter(artifact -> artifact.getGroupId().equals(groupId))
.collect(Collectors.toList());
}
}

0 comments on commit e924e47

Please sign in to comment.