Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Maven version in the dev mojo #1379

Merged
merged 4 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@

<!-- Enable APT by default for Eclipse -->
<m2e.apt.activation>jdt_apt</m2e.apt.activation>

<!--
Supported Maven versions, interpreted as a version range
-->
<supported-maven-versions>[3.5.3,)</supported-maven-versions>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -621,7 +626,7 @@
<rules>
<dependencyConvergence/>
<requireMavenVersion>
<version>3.5.3</version>
<version>${supported-maven-versions}</version>
</requireMavenVersion>
<bannedDependencies>
<excludes>
Expand Down
2 changes: 2 additions & 0 deletions devtools/common/src/main/filtered/quarkus.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ kotlin-version=${kotlin.version}
plugin-groupId=${project.groupId}
plugin-artifactId=quarkus-maven-plugin
plugin-version=${project.version}

supported-maven-versions=${supported-maven-versions}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.quarkus.SourceType;
import io.quarkus.cli.commands.AddExtensions;
import io.quarkus.cli.commands.CreateProject;
import io.quarkus.maven.components.MavenVersionEnforcer;
import io.quarkus.maven.components.Prompter;
import io.quarkus.maven.utilities.MojoUtils;

Expand Down Expand Up @@ -79,8 +80,15 @@ public class CreateProjectMojo extends AbstractMojo {
@Component
private Prompter prompter;

@Component
private MavenVersionEnforcer mavenVersionEnforcer;

@Override
public void execute() throws MojoExecutionException {
// We detect the Maven version during the project generation to indicate the user immediately that the installed
// version may not be supported.
mavenVersionEnforcer.ensureMavenVersion(getLog(), session);

File projectRoot = new File(".");
File pom = new File(projectRoot, "pom.xml");

Expand Down
26 changes: 14 additions & 12 deletions devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
Expand All @@ -36,19 +31,20 @@
import java.util.zip.ZipOutputStream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;

import io.quarkus.dev.ClassLoaderCompiler;
import io.quarkus.dev.DevModeMain;
import io.quarkus.maven.components.MavenVersionEnforcer;
import io.quarkus.maven.utilities.MojoUtils;

/**
Expand Down Expand Up @@ -107,6 +103,12 @@ public class DevMojo extends AbstractMojo {
@Parameter(defaultValue = "${jvm.args}")
private String jvmArgs;

@Parameter(defaultValue = "${session}")
private MavenSession session;

@Component
private MavenVersionEnforcer mavenVersionEnforcer;

/**
* This value is intended to be set to true when some generated bytecode
* is erroneous causing the JVM to crash when the verify:none option is set (which is on by default)
Expand All @@ -115,8 +117,8 @@ public class DevMojo extends AbstractMojo {
private boolean preventnoverify = false;

@Override
public void execute() throws MojoFailureException {

public void execute() throws MojoFailureException, MojoExecutionException {
mavenVersionEnforcer.ensureMavenVersion(getLog(), session);
boolean found = false;
for (Plugin i : project.getBuildPlugins()) {
if (i.getGroupId().equals(MojoUtils.getPluginGroupId())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.quarkus.maven.components;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.versioning.*;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.component.annotations.Component;

import io.quarkus.maven.utilities.MojoUtils;

@Component(role = MavenVersionEnforcer.class, instantiationStrategy = "per-lookup")
public class MavenVersionEnforcer {

public void ensureMavenVersion(Log log, MavenSession session) throws MojoExecutionException {
String supported = MojoUtils.get("supported-maven-versions");
String mavenVersion = session.getSystemProperties().getProperty("maven.version");
log.debug("Detected Maven Version: " + mavenVersion);
DefaultArtifactVersion detectedVersion = new DefaultArtifactVersion(mavenVersion);
enforce(log, supported, detectedVersion);
}

/**
* Compares the specified Maven version to see if it is allowed by the defined version range.
*
* @param log the log
* @param requiredMavenVersionRange range of allowed versions for Maven.
* @param actualMavenVersion the version to be checked.
* @throws MojoExecutionException the given version fails the enforcement rule
*/
private void enforce(Log log,
String requiredMavenVersionRange, ArtifactVersion actualMavenVersion)
throws MojoExecutionException {
if (StringUtils.isBlank(requiredMavenVersionRange)) {
throw new MojoExecutionException("Maven version can't be empty.");
} else {
VersionRange vr;
String msg = "Detected Maven Version (" + actualMavenVersion + ") ";

if (actualMavenVersion.toString().equals(requiredMavenVersionRange)) {
log.debug(msg + " is allowed in " + requiredMavenVersionRange + ".");
} else {
try {
vr = VersionRange.createFromVersionSpec(requiredMavenVersionRange);
if (containsVersion(vr, actualMavenVersion)) {
log.debug(msg + " is allowed in " + requiredMavenVersionRange + ".");
} else {
String message = msg + " is not supported, it must be in " + vr + ".";
throw new MojoExecutionException(message);
}
} catch (InvalidVersionSpecificationException e) {
throw new MojoExecutionException("The requested Maven version "
+ requiredMavenVersionRange + " is invalid.", e);
}
}
}
}

/**
* Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
* containsVersion method assumes a singular version means allow everything.
*
* @param allowedRange range of allowed versions.
* @param theVersion the version to be checked.
* @return {@code true} if the version is contained by the range.
*/
private static boolean containsVersion(VersionRange allowedRange, ArtifactVersion theVersion) {
boolean matched = false;
ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
if (recommendedVersion == null) {
List<Restriction> restrictions = allowedRange.getRestrictions();
for (Restriction restriction : restrictions) {
if (restriction.containsVersion(theVersion)) {
matched = true;
break;
}
}
} else {
// only singular versions ever have a recommendedVersion
int compareTo = recommendedVersion.compareTo(theVersion);
matched = (compareTo <= 0);
}
return matched;
}
}