Skip to content

Commit

Permalink
Add functionality marge for build extensions section (#54)
Browse files Browse the repository at this point in the history
* Add functionality marge for build extensions section
  • Loading branch information
Fameing authored May 9, 2024
1 parent 2c43c89 commit a6bcef0
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 40 deletions.
9 changes: 9 additions & 0 deletions examples/mixin-maven-plugin/mixin-example/mixin1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
<message>HELLO DEFAULT MESSAGE FROM MIXIN</message>
</properties>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.extensions</groupId>
<artifactId>maven-build-cache-extension</artifactId>
<version>1.0.1</version>
</extension>
</extensions>


<pluginManagement>
<plugins>
<!-- This is a simple execution of antrun during initialize phase to echo a message from a property.
Expand Down
12 changes: 10 additions & 2 deletions examples/mixin-maven-plugin/mixin-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<name>${project.groupId}:${project.artifactId}</name>

<properties>
<version.com.playtika.maven.plugins>1.0.1</version.com.playtika.maven.plugins>
<mixin-maven-plugin.version>1.0.5-SNAPSHOT</mixin-maven-plugin.version>
</properties>

<modules>
<module>mixin1</module>
<module>mixin2</module>
Expand All @@ -21,9 +22,16 @@
<plugin>
<groupId>com.playtika.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<version>${version.com.playtika.maven.plugins}</version>
<version>${mixin-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<!--
How to test :
1. build all mixin
2. locate to mixin-consumer
3. Run: mvn -Dmixin-maven-plugin.version=1.0.4 help:effective-pom -U
-->
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;

import java.util.Arrays;
import java.util.Collections;

@Component(role = ArtifactFetcher.class, hint = "DefaultArtifactFetcher")
public class DefaultArtifactFetcher implements ArtifactFetcher {
Expand All @@ -20,7 +20,6 @@ public class DefaultArtifactFetcher implements ArtifactFetcher {
@Requirement
private ResolutionErrorHandler resolutionErrorHandler;


@Override
public Artifact createArtifact(String groupId, String artifactId, String type, String classifier, String version) {
return repositorySystem.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
Expand All @@ -30,7 +29,7 @@ public Artifact createArtifact(String groupId, String artifactId, String type, S
public void resolve(Artifact artifact, MavenSession session) throws ArtifactResolutionException {
ArtifactResolutionRequest request = new ArtifactResolutionRequest();
request.setArtifact(artifact);
request.setCollectionFilter(new IncludesArtifactFilter(Arrays.asList((new String[]{artifact.getId()}))));
request.setCollectionFilter(new IncludesArtifactFilter(Collections.singletonList(artifact.getId())));

request.setResolveRoot(true)
.setResolveTransitively(true)
Expand All @@ -46,5 +45,4 @@ public void resolve(Artifact artifact, MavenSession session) throws ArtifactReso
ArtifactResolutionResult result = repositorySystem.resolve(request);
resolutionErrorHandler.throwErrors(request, result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.playtika.maven.plugins;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.util.ArrayList;
import java.util.List;

/**
* This mojo is a fake mojo just for having the mixins configuration available in IDEs such as intelliJ
*
* @author odavid
*/
@Mojo(name = "fake-mixin-mojo", threadSafe = true)
public class FakeMixinMojo extends AbstractMojo {

@Parameter(property = "mixins")
private List<Mixin> mixins = new ArrayList<>();

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
//DO NOTHING...
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@
import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

public class Mixin {
@Parameter(property = "groupId")
private String groupId;
@Parameter(property = "artifactId")
private String artifactId;
@Parameter(property = "version")
private String version;
@Parameter(property = "type")
private String type;

@Parameter(property = "mergePluginManagement")
private Boolean mergePluginManagement;
@Parameter(property = "mergePlugins")
private Boolean mergePlugins;
@Parameter(property = "mergeReporting")
private Boolean mergeReporting;
@Parameter(property = "mergeProperties")
private Boolean mergeProperties;
@Parameter(property = "mergeDistributionManagement")
private Boolean mergeDistributionManagement;
@Parameter(property = "mergeScm")
private Boolean mergeScm;
@Parameter(property = "mergeRepositories")
private Boolean mergeRepositories;
@Parameter(property = "mergePluginRepositories")
private Boolean mergePluginRepositories;
@Parameter(property = "mergeBuildExtensions")
private Boolean mergeBuildExtensions;
private Boolean recurse;
private Boolean activateProfiles;
private Mixins mixins;
Expand Down Expand Up @@ -93,6 +108,10 @@ public boolean isMergeDistributionManagement() {
return mergeDistributionManagement != null ? mergeDistributionManagement : mixins.isMergeDistributionManagement();
}

public void setMergeDistributionManagement(Boolean mergeDistributionManagement) {
this.mergeDistributionManagement = mergeDistributionManagement;
}

public boolean isMergeScm() {
return mergeScm != null ? mergeScm : mixins.isMergeScm();
}
Expand All @@ -117,6 +136,14 @@ public void setMergePluginRepositories(Boolean mergePluginRepositories) {
this.mergePluginRepositories = mergePluginRepositories;
}

public boolean isMergeBuildExtensions() {
return mergeBuildExtensions != null ? mergeBuildExtensions : mixins.isMergeBuildExtensions();
}

public void setMergeBuildExtensions(Boolean mergeBuildExtensions) {
this.mergeBuildExtensions = mergeBuildExtensions;
}

public boolean isRecurse() {
return recurse != null ? recurse : mixins.isRecurse();
}
Expand Down Expand Up @@ -168,11 +195,13 @@ public void merge(Model mixinModel, MavenProject mavenProject, MavenSession mave
if (isMergePluginRepositories()) {
mixinModelMerger.mergePluginRepositories(mavenProject.getModel(), mixinModel);
}
if (isMergeBuildExtensions()) {
mixinModelMerger.mergeBuildExtensions(mavenProject.getModel(), mixinModel);
}
}

@Override
public String toString() {
return getKey();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,19 @@ public void afterProjectsRead(MavenSession mavenSession) throws MavenExecutionEx

List<MavenProject> projects = mavenSession.getProjects();
for (MavenProject module : projects) {
MixinsProjectLoader loader = new MixinsProjectLoader(mavenSession, module,
modelInterpolator, pluginConfigurationExpander, beanConfigurator, logger, mixinModelCache, profileSelector, profileInjector, mixinModelMerger, reportingConverter, repositorySystem);
MixinsProjectLoader loader = new MixinsProjectLoader(mavenSession,
module,
modelInterpolator,
pluginConfigurationExpander,
beanConfigurator,
logger,
mixinModelCache,
profileSelector,
profileInjector,
mixinModelMerger,
reportingConverter,
repositorySystem
);
loader.mergeMixins();
}
mavenSession.setProjects(projects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class MixinModelMerger extends MavenModelMerger {

public void mergePluginManagement(Model target, Model source) {
Map<Object, Object> context = new HashMap<Object, Object>();
Map<Object, Object> context = new HashMap<>();
if (source.getBuild() != null && source.getBuild().getPluginManagement() != null) {
if (target.getBuild() == null) {
target.setBuild(new Build());
Expand Down Expand Up @@ -91,12 +91,12 @@ public void mergeReporting(Model target, Model source) {
}

public void applyPluginManagementOnPlugins(Model model) {
Map<Object, Object> context = new HashMap<Object, Object>();
Map<Object, Object> context = new HashMap<>();
mergePluginContainers(model.getBuild(), model.getBuild().getPluginManagement(), context, false);
}

public void mergePlugins(Model target, Model source) {
Map<Object, Object> context = new HashMap<Object, Object>();
Map<Object, Object> context = new HashMap<>();
if (source.getBuild() != null) {
if (target.getBuild() == null) {
target.setBuild(new Build());
Expand All @@ -108,14 +108,14 @@ public void mergePlugins(Model target, Model source) {
}

public void mergeProperties(Model target, Model source) {
Map<Object, Object> context = new HashMap<Object, Object>();
Map<Object, Object> context = new HashMap<>();
if (source.getProperties() != null) {
super.mergeModelBase_Properties(target, source, false, context);
}
}

public void mergeDistributionManagement(Model target, Model source) {
Map<Object, Object> context = new HashMap<Object, Object>();
Map<Object, Object> context = new HashMap<>();
if (source.getDistributionManagement() != null) {
if (target.getDistributionManagement() == null) {
target.setDistributionManagement(new DistributionManagement());
Expand Down Expand Up @@ -187,4 +187,10 @@ public void mergePluginRepositories(Model targetModel, Model sourceModel) {
super.mergeModelBase_PluginRepositories(targetModel, sourceModel, false, context);
}

public void mergeBuildExtensions(Model targetModel, Model sourceModel) {
Map<Object, Object> context = new HashMap<Object, Object>();
Build targetBuild = targetModel.getBuild();
Build sourceBuild = sourceModel.getBuild();
super.mergeBuild_Extensions(targetBuild, sourceBuild, false, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
import java.util.Set;

public class MixinModelProblemCollector implements ModelProblemCollector {
private final Set<ModelProblem.Severity> SEVERITIES = EnumSet.noneOf(ModelProblem.Severity.class);
List<ModelProblemCollectorRequest> problems = new ArrayList<>();
private Set<ModelProblem.Severity> severities = EnumSet.noneOf(ModelProblem.Severity.class);

@Override
public void add(ModelProblemCollectorRequest req) {
problems.add(req);
severities.add(req.getSeverity());
SEVERITIES.add(req.getSeverity());
}

public void clear() {
problems.clear();
severities.clear();
SEVERITIES.clear();
}

public void checkErrors(File pom) throws MavenExecutionException {
if (severities.contains(ModelProblem.Severity.ERROR) || severities.contains(ModelProblem.Severity.FATAL)) {
if (SEVERITIES.contains(ModelProblem.Severity.ERROR) || SEVERITIES.contains(ModelProblem.Severity.FATAL)) {
PrintWriter out = new PrintWriter(new StringWriter());
for (ModelProblemCollectorRequest request : problems) {
out.printf("Model Problem: %s%n", request.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.playtika.maven.plugins;

import org.apache.maven.plugins.annotations.Parameter;

import java.util.ArrayList;
import java.util.List;

public class Mixins {
private List<Mixin> mixins = new ArrayList<Mixin>();
private String defaultMixinType = "pom";

@Parameter(property = "mergePlugins")
private boolean mergePlugins = true;
@Parameter(property = "mergePluginManagement")
private boolean mergePluginManagement = true;
@Parameter(property = "mergeBuildExtensions")
private boolean mergeBuildExtensions = true;
@Parameter(property = "mergeProperties")
private boolean mergeProperties = true;
@Parameter(property = "mergeReporting")
private boolean mergeReporting = true;
@Parameter(property = "mergeDistributionManagement")
private boolean mergeDistributionManagement = true;
private boolean recurse = true;
private boolean activateProfiles = true;
private String defaultMixinType = "pom";
@Parameter(property = "mergeScm")
private boolean mergeScm = true;
@Parameter(property = "mergeRepositories")
private boolean mergeRepositories = true;
@Parameter(property = "mergePluginRepositories")
private boolean mergePluginRepositories = true;

public String getDefaultMixinType() {
Expand Down Expand Up @@ -90,7 +103,6 @@ public void setMergeReporting(boolean mergeReporting) {
this.mergeReporting = mergeReporting;
}


public boolean isMergeScm() {
return mergeScm;
}
Expand All @@ -111,6 +123,14 @@ public boolean isMergePluginRepositories() {
return mergePluginRepositories;
}

public boolean isMergeBuildExtensions() {
return mergeBuildExtensions;
}

public void setMergeBuildExtensions(boolean mergeBuildExtensions) {
this.mergeBuildExtensions = mergeBuildExtensions;
}

public void setMergePluginRepositories(boolean mergePluginRepositories) {
this.mergePluginRepositories = mergePluginRepositories;
}
Expand Down
Loading

0 comments on commit a6bcef0

Please sign in to comment.