Skip to content

Commit 3274b49

Browse files
authored
Merge pull request #142 from sghill/fix/applied-to-multiple-projects
Share copyCount AtomicInteger to avoid duplicate configurations
2 parents f70815a + 8375859 commit 3274b49

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/main/groovy/netflix/nebula/dependency/recommender/DependencyRecommendationsPlugin.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545

4646
import java.lang.reflect.Method;
4747
import java.util.*;
48+
import java.util.concurrent.atomic.AtomicInteger;
4849

4950
public class DependencyRecommendationsPlugin implements Plugin<Project> {
5051
public static final String NEBULA_RECOMMENDER_BOM = "nebulaRecommenderBom";
5152
public static final boolean CORE_BOM_SUPPORT_ENABLED = Boolean.getBoolean("nebula.features.coreBomSupport");
5253
private static final GradleVersion GRADLE_9_0 = GradleVersion.version("9.0");
54+
private static final AtomicInteger COPY_COUNT = new AtomicInteger();
5355
private Logger logger = Logging.getLogger(DependencyRecommendationsPlugin.class);
5456
private RecommendationProviderContainer recommendationProviderContainer;
5557
//TODO: remove this exclusion once https://github.com/gradle/gradle/issues/6750 is resolved
@@ -84,15 +86,15 @@ public void execute(Project p) {
8486
BomResolutionUtil.eagerlyResolveBoms(p, recommendationProviderContainer, NEBULA_RECOMMENDER_BOM);
8587
}
8688

87-
p.getConfigurations().all(new ExtendRecommenderConfigurationAction(bomConfiguration, p, recommendationProviderContainer));
89+
p.getConfigurations().all(new ExtendRecommenderConfigurationAction(bomConfiguration, p, recommendationProviderContainer, COPY_COUNT));
8890
p.subprojects(new Action<Project>() {
8991
@Override
9092
public void execute(Project sub) {
9193
// Also eagerly resolve BOMs for subprojects if using build service
9294
if (shouldUseBuildService(sub) && BomResolutionUtil.shouldEagerlyResolveBoms(sub, recommendationProviderContainer)) {
9395
BomResolutionUtil.eagerlyResolveBoms(sub, recommendationProviderContainer, NEBULA_RECOMMENDER_BOM);
9496
}
95-
sub.getConfigurations().all(new ExtendRecommenderConfigurationAction(bomConfiguration, sub, recommendationProviderContainer));
97+
sub.getConfigurations().all(new ExtendRecommenderConfigurationAction(bomConfiguration, sub, recommendationProviderContainer, COPY_COUNT));
9698
}
9799
});
98100
}

src/main/groovy/netflix/nebula/dependency/recommender/ExtendRecommenderConfigurationAction.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ public class ExtendRecommenderConfigurationAction implements Action<Configuratio
2424
private final Configuration bom;
2525
private final Project project;
2626
private final RecommendationProviderContainer container;
27+
private final AtomicInteger copyCount;
2728

28-
private final AtomicInteger copyCount = new AtomicInteger();
29-
30-
public ExtendRecommenderConfigurationAction(Configuration bom, Project project, RecommendationProviderContainer container) {
29+
public ExtendRecommenderConfigurationAction(Configuration bom, Project project, RecommendationProviderContainer container, AtomicInteger copyCount) {
3130
this.bom = bom;
3231
this.project = project;
3332
this.container = container;
33+
this.copyCount = copyCount;
34+
}
35+
36+
37+
public ExtendRecommenderConfigurationAction(Configuration bom, Project project, RecommendationProviderContainer container) {
38+
this(bom, project, container, new AtomicInteger());
3439
}
3540

3641
@Override

src/test/groovy/netflix/nebula/dependency/recommender/DependencyRecommendationsPluginMultiprojectCoreBomSupportSpec.groovy

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,41 @@ class DependencyRecommendationsPluginMultiprojectCoreBomSupportSpec extends Inte
8585
then:
8686
results.output.contains("+--- test.nebula:foo -> 1.0.0")
8787
}
88+
89+
def 'can use recommender applied to multiple projects'() {
90+
def a = addSubproject('a', '''\
91+
dependencies {
92+
implementation 'test.nebula:foo'
93+
}
94+
'''.stripIndent())
95+
writeHelloWorld('a', a)
96+
def b = addSubproject('b', '''\
97+
dependencies {
98+
implementation project(':a')
99+
}
100+
'''.stripIndent())
101+
writeHelloWorld('b', b)
102+
buildFile << """\
103+
allprojects {
104+
apply plugin: 'com.netflix.nebula.dependency-recommender'
105+
}
106+
dependencyRecommendations {
107+
mavenBom module: 'test.nebula.bom:testbom:latest.release'
108+
}
109+
110+
allprojects {
111+
apply plugin: 'java'
112+
113+
repositories {
114+
maven { url = '${repo.root.absoluteFile.toURI()}' }
115+
${generator.mavenRepositoryBlock}
116+
}
117+
}
118+
""".stripIndent()
119+
when:
120+
def results = runTasks(':a:dependencies', '--configuration', 'compileClasspath')
121+
122+
then:
123+
results.output.contains("+--- test.nebula:foo -> 1.0.0")
124+
}
88125
}

0 commit comments

Comments
 (0)