From 391da7c643860a22cbb361b9d69d04de4948457f Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Mon, 22 Mar 2021 15:48:58 +0100 Subject: [PATCH] Fixes quarkus-platform native TS config --- .../quarkus/maven/QuarkusBootstrapMojo.java | 30 ++++ .../maven/QuarkusBootstrapProvider.java | 128 +++++++++++++----- 2 files changed, 127 insertions(+), 31 deletions(-) diff --git a/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java index 1af17c8a29623..a090181762228 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java @@ -72,6 +72,30 @@ public abstract class QuarkusBootstrapMojo extends AbstractMojo { @Parameter(property = "ignoredEntries") private String[] ignoredEntries; + /** + * Coordinates of the Maven artifact containing the original Java application to build the native image for. + * If not provided, the current project is assumed to be the original Java application. + *

+ * The coordinates are expected to be expressed in the following format: + *

+ * groupId:artifactId:classifier:type:version + *

+ * With the classifier, type and version being optional. + *

+ * If the type is missing, the artifact is assumed to be of type JAR. + *

+ * If the version is missing, the artifact is going to be looked up among the project dependencies using the provided + * coordinates. + * + *

+ * However, if the expression consists of only three parts, it is assumed to be groupId:artifactId:version. + * + *

+ * If the expression consists of only four parts, it is assumed to be groupId:artifactId:classifier:type. + */ + @Parameter(required = false, property = "appArtifact") + private String appArtifact; + private AppArtifactKey projectId; @Override @@ -105,6 +129,10 @@ public void setLog(Log log) { */ protected abstract void doExecute() throws MojoExecutionException, MojoFailureException; + protected String appArtifactCoords() { + return appArtifact; + } + protected RepositorySystem repositorySystem() { return bootstrapProvider.repositorySystem(); } @@ -145,6 +173,8 @@ protected AppArtifactKey projectId() { return projectId == null ? projectId = new AppArtifactKey(project.getGroupId(), project.getArtifactId()) : projectId; } + // @deprecated in 1.14.0.Final + @Deprecated protected AppArtifact projectArtifact() throws MojoExecutionException { return bootstrapProvider.projectArtifact(this); } diff --git a/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapProvider.java b/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapProvider.java index c827cd3f450b3..e03e3efa055b6 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapProvider.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapProvider.java @@ -12,6 +12,7 @@ import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.impl.RemoteRepositoryManager; import com.google.common.cache.Cache; @@ -60,9 +61,11 @@ public MavenArtifactResolver artifactResolver(QuarkusBootstrapMojo mojo) return provider(mojo.projectId()).artifactResolver(mojo); } + // @deprecated in 1.14.0.Final + @Deprecated public AppArtifact projectArtifact(QuarkusBootstrapMojo mojo) throws MojoExecutionException { - return provider(mojo.projectId()).projectArtifact(mojo); + return provider(mojo.projectId()).appArtifact(mojo); } public QuarkusBootstrap bootstrapQuarkus(QuarkusBootstrapMojo mojo) @@ -91,7 +94,7 @@ public void close() throws IOException { private class QuarkusAppBootstrapProvider implements Closeable { - private AppArtifact projectArtifact; + private AppArtifact appArtifact; private MavenArtifactResolver artifactResolver; private QuarkusBootstrap quarkusBootstrap; private CuratedApplication curatedApp; @@ -114,34 +117,6 @@ private MavenArtifactResolver artifactResolver(QuarkusBootstrapMojo mojo) } } - private AppArtifact projectArtifact(QuarkusBootstrapMojo mojo) throws MojoExecutionException { - if (projectArtifact != null) { - return projectArtifact; - } - final Artifact projectArtifact = mojo.mavenProject().getArtifact(); - final AppArtifact appArtifact = new AppArtifact(projectArtifact.getGroupId(), projectArtifact.getArtifactId(), - projectArtifact.getClassifier(), projectArtifact.getArtifactHandler().getExtension(), - projectArtifact.getVersion()); - - File projectFile = projectArtifact.getFile(); - if (projectFile == null) { - projectFile = new File(mojo.mavenProject().getBuild().getOutputDirectory()); - if (!projectFile.exists()) { - /* - * TODO GenerateCodeMojo would fail - * if (hasSources(project)) { - * throw new MojoExecutionException("Project " + project.getArtifact() + " has not been compiled yet"); - * } - */ - if (!projectFile.mkdirs()) { - throw new MojoExecutionException("Failed to create the output dir " + projectFile); - } - } - } - appArtifact.setPaths(PathsCollection.of(projectFile.toPath())); - return appArtifact; - } - protected QuarkusBootstrap bootstrapQuarkus(QuarkusBootstrapMojo mojo) throws MojoExecutionException { if (quarkusBootstrap != null) { return quarkusBootstrap; @@ -164,7 +139,8 @@ protected QuarkusBootstrap bootstrapQuarkus(QuarkusBootstrapMojo mojo) throws Mo effectiveProperties.putIfAbsent("quarkus.application.version", mojo.mavenProject().getVersion()); QuarkusBootstrap.Builder builder = QuarkusBootstrap.builder() - .setAppArtifact(projectArtifact(mojo)) + .setAppArtifact(appArtifact(mojo)) + .setManagingProject(managingProject(mojo)) .setMavenArtifactResolver(artifactResolver(mojo)) .setIsolateDeployment(true) .setBaseClassLoader(getClass().getClassLoader()) @@ -195,6 +171,96 @@ protected CuratedApplication curateApplication(QuarkusBootstrapMojo mojo) throws } } + protected AppArtifact managingProject(QuarkusBootstrapMojo mojo) { + if (mojo.appArtifactCoords() == null) { + return null; + } + final Artifact artifact = mojo.mavenProject().getArtifact(); + return new AppArtifact(artifact.getGroupId(), artifact.getArtifactId(), + artifact.getClassifier(), artifact.getArtifactHandler().getExtension(), + artifact.getVersion()); + } + + private AppArtifact appArtifact(QuarkusBootstrapMojo mojo) throws MojoExecutionException { + return appArtifact == null ? appArtifact = initAppArtifact(mojo) : appArtifact; + } + + private AppArtifact initAppArtifact(QuarkusBootstrapMojo mojo) throws MojoExecutionException { + String appArtifactCoords = mojo.appArtifactCoords(); + if (appArtifactCoords == null) { + final Artifact projectArtifact = mojo.mavenProject().getArtifact(); + final AppArtifact appArtifact = new AppArtifact(projectArtifact.getGroupId(), projectArtifact.getArtifactId(), + projectArtifact.getClassifier(), projectArtifact.getArtifactHandler().getExtension(), + projectArtifact.getVersion()); + + File projectFile = projectArtifact.getFile(); + if (projectFile == null) { + projectFile = new File(mojo.mavenProject().getBuild().getOutputDirectory()); + if (!projectFile.exists()) { + /* + * TODO GenerateCodeMojo would fail + * if (hasSources(project)) { + * throw new MojoExecutionException("Project " + project.getArtifact() + " has not been compiled yet"); + * } + */ + if (!projectFile.mkdirs()) { + throw new MojoExecutionException("Failed to create the output dir " + projectFile); + } + } + } + appArtifact.setPaths(PathsCollection.of(projectFile.toPath())); + return appArtifact; + } + + final String[] coordsArr = appArtifactCoords.split(":"); + if (coordsArr.length < 2 || coordsArr.length > 5) { + throw new MojoExecutionException( + "appArtifact expression " + appArtifactCoords + + " does not follow format groupId:artifactId:classifier:type:version"); + } + final String groupId = coordsArr[0]; + final String artifactId = coordsArr[1]; + String classifier = ""; + String type = "jar"; + String version = null; + if (coordsArr.length == 3) { + version = coordsArr[2]; + } else if (coordsArr.length > 3) { + classifier = coordsArr[2] == null ? "" : coordsArr[2]; + type = coordsArr[3] == null ? "jar" : coordsArr[3]; + if (coordsArr.length > 4) { + version = coordsArr[4]; + } + } + if (version == null) { + for (Artifact dep : mojo.mavenProject().getArtifacts()) { + if (dep.getArtifactId().equals(artifactId) + && dep.getGroupId().equals(groupId) + && dep.getClassifier().equals(classifier) + && dep.getType().equals(type)) { + return new AppArtifact(dep.getGroupId(), + dep.getArtifactId(), + dep.getClassifier(), + dep.getArtifactHandler().getExtension(), + dep.getVersion()); + } + } + throw new IllegalStateException("Failed to locate " + appArtifactCoords + " among the project dependencies"); + } + + final AppArtifact appArtifact = new AppArtifact(groupId, artifactId, classifier, type, version); + try { + appArtifact.setPath( + artifactResolver(mojo).resolve(new DefaultArtifact(groupId, artifactId, classifier, type, version)) + .getArtifact().getFile().toPath()); + } catch (MojoExecutionException e) { + throw e; + } catch (Exception e) { + throw new MojoExecutionException("Failed to resolve " + appArtifact, e); + } + return appArtifact; + } + @Override public void close() throws IOException { if (curatedApp != null) {