Skip to content

Commit

Permalink
Fix for WFMP-269, Support multiple deployments in the package goal
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdenise committed Sep 9, 2024
1 parent bdfb5ed commit e67c069
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 21 deletions.
6 changes: 3 additions & 3 deletions plugin/src/main/java/org/wildfly/plugin/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static ScanResults scanDeployment(GlowConfig discoverProvisioningInfo,
List<GalleonFeaturePack> featurePacks,
boolean dryRun,
Log log,
Path deploymentContent,
List<Path> deploymentContents,
MavenRepoManager artifactResolver,
Path outputFolder,
GalleonBuilder pm,
Expand All @@ -119,7 +119,7 @@ public static ScanResults scanDeployment(GlowConfig discoverProvisioningInfo,
if (!excludedLayers.isEmpty()) {
throw new MojoExecutionException("excluded layers must be empty when enabling glow");
}
if (!Files.exists(deploymentContent)) {
if (deploymentContents.isEmpty()) {
throw new MojoExecutionException("A deployment is expected when enabling glow layer discovery");
}
Path inProvisioningFile = null;
Expand All @@ -134,7 +134,7 @@ public static ScanResults scanDeployment(GlowConfig discoverProvisioningInfo,
p.storeProvisioningConfig(in, inProvisioningFile);
}
}
Arguments arguments = discoverProvisioningInfo.toArguments(deploymentContent, inProvisioningFile,
Arguments arguments = discoverProvisioningInfo.toArguments(deploymentContents, inProvisioningFile,
layersConfigurationFileName);
log.info("Glow is scanning... ");
ScanResults results;
Expand Down
4 changes: 3 additions & 1 deletion plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,12 @@ protected Path provisionIfRequired(final Path installDir) throws MojoFailureExce
}

private ScanResults scanDeployment(GalleonBuilder pm) throws Exception {
List<Path> lst = new ArrayList<>();
lst.add(resolveWarLocation());
return Utils.scanDeployment(discoverProvisioningInfo,
layers, excludedLayers, featurePacks, false,
getLog(),
resolveWarLocation(),
lst,
mavenRepoManager,
Paths.get(project.getBuild().getDirectory()),
pm,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.plugin.provision;

/**
*
* @author jdenise
*/
public class DeploymentConfiguration {

private String groupId;
private String artifactId;
private String classifier;
private String type = "jar";
private String name;

public String getName() {
return name;
}

public String getGroupId() {
return groupId;
}

public String getArtifactId() {
return artifactId;
}

public String getType() {
return type;
}

public String getClassifier() {
return classifier;
}

/**
* @param groupId the groupId to set
*/
public void setGroupId(String groupId) {
this.groupId = groupId;
}

/**
* @param artifactId the artifactId to set
*/
public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}

/**
* @param classifier the classifier to set
*/
public void setClassifier(String classifier) {
this.classifier = classifier;
}

/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.plugin.provision;

import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

/**
* Resolve possible deployments from the Maven project dependencies.
*/
class DeploymentResolution {

private static final String WAR = "war";
private static final String EAR = "ear";
private static final String RAR = "rar";
private static final String JAR = "jar";
private static final Set<String> SUPPORTED_DEPLOYMENT_TYPES = Set.of(EAR, JAR, RAR, WAR);
private static final String TEST_JAR = "test-jar";

static DeploymentResolution getInstance(MavenProject project) throws MojoExecutionException {
return new DeploymentResolution(project);
}

private final Map<String, File> files = new TreeMap<>();

private DeploymentResolution(MavenProject project) throws MojoExecutionException {
for (Artifact artifact : project.getArtifacts()) {
if (TEST_JAR.equals(artifact.getType())) {
continue;
}
if (SUPPORTED_DEPLOYMENT_TYPES.contains(artifact.getType())) {
files.put(buildKey(artifact.getGroupId(), artifact.getArtifactId(),
artifact.getClassifier(), artifact.getType()), artifact.getFile());
}
}
}

public File getFile(final String groupId, final String artifactId, final String classifier,
final String type) throws MojoExecutionException {
return files.get(buildKey(groupId, artifactId, classifier, type));
}

private String buildKey(String groupId, String artifactId, String classifier, String type) throws MojoExecutionException {
if (groupId == null) {
throw new MojoExecutionException("Deployment groupId can't be null");
}
if (artifactId == null) {
throw new MojoExecutionException("Deployment artifactId can't be null");
}
if (type == null) {
throw new MojoExecutionException("Deployment type can't be null");
}
if (!SUPPORTED_DEPLOYMENT_TYPES.contains(type)) {
throw new MojoExecutionException(
"Deployment type " + type + " is not supported. Supported types are " + SUPPORTED_DEPLOYMENT_TYPES);
}
return groupId + ":" + artifactId + ":" + (classifier == null ? "" : classifier) + ":" + type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public class GlowConfig {
public GlowConfig() {
}

public Arguments toArguments(Path deployment, Path inProvisioning, String layersConfigurationFileName) {
public Arguments toArguments(List<Path> lst, Path inProvisioning, String layersConfigurationFileName) {
final Set<String> profiles = profile != null ? Set.of(profile) : Set.of();
List<Path> lst = List.of(deployment);
Builder builder = Arguments.scanBuilder().setExecutionContext(context).setExecutionProfiles(profiles)
.setUserEnabledAddOns(addOns).setBinaries(lst).setSuggest(suggest).setJndiLayers(getLayersForJndi())
.setVersion(version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.inject.Inject;

Expand All @@ -31,6 +34,7 @@
import org.jboss.galleon.api.GalleonBuilder;
import org.jboss.galleon.api.config.GalleonProvisioningConfig;
import org.jboss.galleon.maven.plugin.util.MvnMessageWriter;
import org.jboss.galleon.universe.maven.MavenUniverseException;
import org.jboss.galleon.util.IoUtils;
import org.wildfly.glow.ScanResults;
import org.wildfly.plugin.cli.BaseCommandConfiguration;
Expand Down Expand Up @@ -170,7 +174,7 @@ public class PackageServerMojo extends AbstractProvisionServerMojo {
private boolean skip;

/**
* Skip deploying the deployment after the server is provisioned ({@code false} by default).
* Skip deploying the deployments after the server is provisioned ({@code false} by default).
*/
@Parameter(defaultValue = "false", property = PropertyNames.SKIP_PACKAGE_DEPLOYMENT)
protected boolean skipDeployment;
Expand Down Expand Up @@ -256,10 +260,34 @@ public class PackageServerMojo extends AbstractProvisionServerMojo {
@Parameter(alias = "bootable-jar-install-artifact-classifier", property = PropertyNames.BOOTABLE_JAR_INSTALL_CLASSIFIER, defaultValue = BootableJarSupport.BOOTABLE_SUFFIX)
private String bootableJarInstallArtifactClassifier;

/**
* A list of deployments to deploy.
* A deployment is expected to be a Maven project dependency. The dependency type must be one of the following types:
* {@code ear}, {@code jar}, {@code rar} or {@code war}.
* If the deployment is not found or if the type is not supported, an exception is thrown.
* If no deployment name is specified, the name of the resolved artifact file is used.
*
* <pre>
* &lt;deployments&gt;
* &lt;deployment&gt;
* &lt;groupId&gt;deployment groupId&lt;/groupId&gt;
* &lt;artifactId&gt;deployment artifactId&lt;/artifactId&gt;
* &lt;type&gt;ear|jar|rar|war&lt;/type&gt;
* &lt;classifier&gt;Optional classifier&lt;/classifier&gt;
* &lt;name&gt;Optional deployment name&lt;/name&gt;
* &lt;/deployment&gt;
* &lt;/deployments&gt;
* </pre>
*/
@Parameter(alias = "deployments", required = false)
List<DeploymentConfiguration> deployments = new ArrayList<>();

@Inject
private OfflineCommandExecutor commandExecutor;

private GalleonProvisioningConfig config;
private Map<String, Path> deploymentPaths;
private DeploymentResolution deploymentResolution;

@Override
protected GalleonProvisioningConfig getDefaultConfig() throws ProvisioningException {
Expand All @@ -274,13 +302,22 @@ protected GalleonProvisioningConfig buildGalleonConfig(GalleonBuilder pm)
return config;
}
try {
List<Path> allDeployments = new ArrayList<>();
Path primaryDeployment = getDeploymentContent();
if (primaryDeployment != null) {
allDeployments.add(primaryDeployment);
}
Map<String, Path> extraDeps = getDeployments();
if (!extraDeps.isEmpty()) {
allDeployments.addAll(extraDeps.values());
}
try (ScanResults results = Utils.scanDeployment(discoverProvisioningInfo,
layers,
excludedLayers,
featurePacks,
dryRun,
getLog(),
getDeploymentContent(),
allDeployments,
artifactResolver,
Paths.get(project.getBuild().getDirectory()),
pm,
Expand All @@ -305,9 +342,45 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().debug(String.format("Skipping " + getGoal() + " of %s:%s", project.getGroupId(), project.getArtifactId()));
return;
}
deploymentResolution = DeploymentResolution.getInstance(project);
super.execute();
}

private void deploy(Path deploymentContent, String targetName) throws MojoDeploymentException {
if (Files.exists(deploymentContent)) {
Path standaloneDeploymentDir = Path.of(provisioningDir, "standalone", "deployments");
if (!standaloneDeploymentDir.isAbsolute()) {
standaloneDeploymentDir = Path.of(project.getBuild().getDirectory()).resolve(standaloneDeploymentDir);
}
try {
Path deploymentTarget = standaloneDeploymentDir.resolve(targetName);
getLog().info("Copy deployment " + deploymentContent + " to " + deploymentTarget);
Files.copy(deploymentContent, deploymentTarget, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new MojoDeploymentException("Could not copy deployment in provisioned server", e);
}
}
}

private Map<String, Path> getDeployments() throws MavenUniverseException, MojoExecutionException {
if (deploymentPaths == null) {
deploymentPaths = new LinkedHashMap<>();
for (DeploymentConfiguration c : deployments) {
Path p;
File f = deploymentResolution.getFile(c.getGroupId(), c.getArtifactId(), c.getClassifier(),
c.getType());
if (f == null) {
throw new MojoExecutionException("Deployment not found " + c.getGroupId() + ":" + c.getArtifactId()
+ (c.getClassifier() == null ? "" : ":" + c.getClassifier()) + ":" + c.getType());
}
p = f.toPath();
String deploymentName = c.getName() == null ? p.getFileName().toString() : c.getName();
deploymentPaths.put(deploymentName, p);
}
}
return deploymentPaths;
}

@Override
protected void serverProvisioned(Path jbossHome) throws MojoExecutionException, MojoFailureException {
try {
Expand All @@ -325,19 +398,18 @@ protected void serverProvisioned(Path jbossHome) throws MojoExecutionException,
}

if (!skipDeployment) {
final Path deploymentContent = getDeploymentContent();
if (Files.exists(deploymentContent)) {
Path standaloneDeploymentDir = Path.of(provisioningDir, "standalone", "deployments");
if (!standaloneDeploymentDir.isAbsolute()) {
standaloneDeploymentDir = Path.of(project.getBuild().getDirectory()).resolve(standaloneDeploymentDir);
}
try {
Path deploymentTarget = standaloneDeploymentDir.resolve(getDeploymentTargetName());
getLog().info("Copy deployment " + deploymentContent + " to " + deploymentTarget);
Files.copy(deploymentContent, deploymentTarget, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new MojoDeploymentException("Could not copy deployment in provisioned server", e);
Path primaryDeployment = getDeploymentContent();
if (primaryDeployment != null) {
deploy(primaryDeployment, getDeploymentTargetName());
}
// Handle extra deployments
try {
Map<String, Path> extraPaths = getDeployments();
for (Entry<String, Path> p : extraPaths.entrySet()) {
deploy(p.getValue(), p.getKey());
}
} catch (Exception ex) {
throw new MojoExecutionException(ex.getLocalizedMessage(), ex);
}
}

Expand Down Expand Up @@ -492,6 +564,9 @@ private void warnExtraConfig(Path extraContentDir) {

protected Path getDeploymentContent() throws MojoExecutionException {
final PackageType packageType = PackageType.resolve(project);
if (packageType.getPackaging().equals("pom")) {
return null;
}
final String filename;
if (this.filename == null) {
filename = String.format("%s.%s", project.getBuild().getFinalName(), packageType.getFileExtension());
Expand Down Expand Up @@ -522,4 +597,4 @@ private static void cleanupServer(Path jbossHome) throws IOException {
IoUtils.recursiveDelete(log);
}

}
}
Loading

0 comments on commit e67c069

Please sign in to comment.