Skip to content

Commit

Permalink
cleanup for old plugin usage, fixes #47
Browse files Browse the repository at this point in the history
  • Loading branch information
McFoggy committed Dec 23, 2016
1 parent a818205 commit 1f065e5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
49 changes: 5 additions & 44 deletions src/main/java/fr/brouillard/oss/jgitver/JGitverExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
// @formatter:on
package fr.brouillard.oss.jgitver;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

@Component(role = AbstractMavenLifecycleParticipant.class, hint = "jgitver")
public class JGitverExtension extends AbstractMavenLifecycleParticipant {
Expand All @@ -51,57 +50,19 @@ public void afterProjectsRead(MavenSession mavenSession) throws MavenExecutionEx
List<MavenProject> projects = locateProjects(mavenSession, rootProject.getModules());

Map<GAV, String> newProjectVersions = new LinkedHashMap<>();
final Consumer<? super CharSequence> c = cs -> logger.warn(cs.toString());

if (JGitverModelProcessor.class.isAssignableFrom(modelProcessor.getClass())) {
JGitverModelProcessor jGitverModelProcessor = JGitverModelProcessor.class.cast(modelProcessor);
JGitverModelProcessorWorkingConfiguration workingConfiguration = jGitverModelProcessor.getWorkingConfiguration();

if (workingConfiguration == null) {
logger.warn("");
logger.warn("jgitver has changed!");
logger.warn("");
logger.warn("it now requires the usage of maven core extensions instead of standard plugin extensions.");
logger.warn("The plugin must be now declared in a `.mvn/extensions.xml` file.");
logger.warn("");
logger.warn(" read https://github.com/jgitver/jgitver-maven-plugin for further information");
logger.warn("");
throw new MavenExecutionException("detection of jgitver old setting mechanism",
new IllegalStateException("jgitver must now use maven core extensions"));
JGitverUtils.failAsOldMechanism(c);
}

newProjectVersions = workingConfiguration.getNewProjectVersions();
} else {
logger.info("jgitver-maven-plugin is about to change project(s) version(s)");

String newVersion = null;
try {
newVersion = JGitverUtils.calculateVersionForProject(rootProject, mavenSession.getUserProperties(), logger)
.getCalculatedVersion();
} catch (IOException ex) {
throw new MavenExecutionException("failure calculating version from git information", ex);
}

// Let's modify in memory resolved projects model
for (MavenProject project : projects) {
GAV projectGAV = GAV.from(project); // SUPPRESS CHECKSTYLE AbbreviationAsWordInName

logger.debug("about to change in memory POM for: " + projectGAV);
// First the project itself
project.setVersion(newVersion);
logger.debug(" version set to " + newVersion);
VersionRange newVersionRange = VersionRange.createFromVersion(newVersion);
project.getArtifact().setVersionRange(newVersionRange);
logger.debug(" artifact version range set to " + newVersionRange);
newProjectVersions.put(projectGAV, newVersion);

// No need to worry about parent link, because model is in memory
}

try {
JGitverUtils.attachModifiedPomFilesToTheProject(projects, newProjectVersions, mavenSession, logger);
} catch (IOException | XmlPullParserException ex) {
throw new MavenExecutionException("cannot attach updated POMs during project execution", ex);
}
JGitverUtils.failAsOldMechanism(c);
}

newProjectVersions.entrySet().forEach(e -> logger.info(" " + e.getKey().toString() + " -> " + e.getValue()));
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/fr/brouillard/oss/jgitver/JGitverMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import java.util.List;

import org.apache.maven.MavenExecutionException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -53,7 +55,11 @@ public class JGitverMojo extends AbstractMojo {
private Boolean useDirty;

public void execute() throws MojoExecutionException {
getLog().warn("the plugin [jgitver-maven-plugin] should not be executed alone,"
+ " verify <extensions>true</extensions> is set on the plugin configuration");
final Log logger = getLog();
try {
JGitverUtils.failAsOldMechanism(logger::warn);
} catch (MavenExecutionException e) {
throw new MojoExecutionException("cannot use jgitver as maven plugin anymore", e);
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/fr/brouillard/oss/jgitver/JGitverUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
Expand Down Expand Up @@ -233,4 +235,16 @@ public static void attachModifiedPomFilesToTheProject(List<MavenProject> project
logger.debug(" pom file set");
}
}

public static void failAsOldMechanism(Consumer<? super CharSequence> logger) throws MavenExecutionException {
logger.accept("jgitver has changed!");
logger.accept("");
logger.accept("it now requires the usage of maven core extensions (> 3.3.1) instead of standard plugin extensions.");
logger.accept("The plugin must be now declared in a `.mvn/extensions.xml` file.");
logger.accept("");
logger.accept(" read https://github.com/jgitver/jgitver-maven-plugin for further information");
logger.accept("");
throw new MavenExecutionException("detection of jgitver old setting mechanism",
new IllegalStateException("jgitver must now use maven core extensions only"));
}
}

0 comments on commit 1f065e5

Please sign in to comment.