Skip to content

Commit

Permalink
Merge pull request #15929 from aloubyansky/appArtifact-support
Browse files Browse the repository at this point in the history
Fixes quarkus-platform native TS config
  • Loading branch information
gsmet authored Mar 23, 2021
2 parents 1b1d8f9 + 391da7c commit c45b1a6
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* The coordinates are expected to be expressed in the following format:
* <p>
* groupId:artifactId:classifier:type:version
* <p>
* With the classifier, type and version being optional.
* <p>
* If the type is missing, the artifact is assumed to be of type JAR.
* <p>
* If the version is missing, the artifact is going to be looked up among the project dependencies using the provided
* coordinates.
*
* <p>
* However, if the expression consists of only three parts, it is assumed to be groupId:artifactId:version.
*
* <p>
* 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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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())
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c45b1a6

Please sign in to comment.