Search in sources :

Example 1 with UndeployDescription

use of org.wildfly.plugin.core.UndeployDescription in project wildfly-maven-plugin by wildfly.

the class UndeployMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().debug(String.format("Skipping undeploy of %s:%s", project.getGroupId(), project.getArtifactId()));
        return;
    }
    final PackageType packageType = PackageType.resolve(project);
    // Configure the name if it wasn't yet set
    if (name == null) {
        name = String.format("%s.%s", project.getBuild().getFinalName(), packageType.getFileExtension());
    }
    if (checkPackaging && packageType.isIgnored()) {
        getLog().debug(String.format("Ignoring packaging type %s.", packageType.getPackaging()));
    } else {
        final DeploymentResult result;
        try (ModelControllerClient client = createClient();
            MavenModelControllerClientConfiguration configuration = getClientConfiguration()) {
            final boolean failOnMissing = !ignoreMissingDeployment;
            final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
            if (matchPattern == null) {
                result = deploymentManager.undeploy(UndeployDescription.of(name).addServerGroups(getServerGroups()).setFailOnMissing(failOnMissing));
            } else {
                final Set<UndeployDescription> matchedDeployments = findDeployments(deploymentManager, failOnMissing);
                if (matchedDeployments.isEmpty()) {
                    if (failOnMissing) {
                        throw new MojoDeploymentException("No deployments matched the match-pattern %s.", matchPattern);
                    }
                    // nothing to undeploy
                    return;
                }
                result = deploymentManager.undeploy(matchedDeployments);
            }
        } catch (IOException e) {
            throw new MojoFailureException("Failed to execute undeploy goal.", e);
        }
        if (!result.successful()) {
            throw new MojoDeploymentException("Failed to undeploy %s. Reason: %s", name, result.getFailureMessage());
        }
    }
}
Also used : UndeployDescription(org.wildfly.plugin.core.UndeployDescription) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) DeploymentManager(org.wildfly.plugin.core.DeploymentManager) MavenModelControllerClientConfiguration(org.wildfly.plugin.common.MavenModelControllerClientConfiguration) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DeploymentResult(org.wildfly.plugin.core.DeploymentResult) IOException(java.io.IOException)

Example 2 with UndeployDescription

use of org.wildfly.plugin.core.UndeployDescription in project wildfly-maven-plugin by wildfly.

the class UndeployMojo method findDeployments.

private Set<UndeployDescription> findDeployments(final DeploymentManager deploymentManager, final boolean failOnMissing) throws IOException, MojoDeploymentException {
    if (name == null && matchPattern == null) {
        throw new IllegalArgumentException("deploymentName and matchPattern are null. One of them must " + "be set in order to find an existing deployment.");
    }
    final MatchPatternStrategy matchPatternStrategy = getMatchPatternStrategy();
    final Set<UndeployDescription> matchedDeployments = new TreeSet<>();
    final Collection<DeploymentDescription> deployments = deploymentManager.getDeployments();
    final Pattern pattern = Pattern.compile(matchPattern);
    for (DeploymentDescription deployment : deployments) {
        boolean matchFound = false;
        final String deploymentName = deployment.getName();
        final Collection<String> serverGroups = getServerGroups();
        if (pattern.matcher(deploymentName).matches()) {
            if (serverGroups.isEmpty()) {
                matchFound = true;
                matchedDeployments.add(UndeployDescription.of(deploymentName).setFailOnMissing(failOnMissing));
            } else {
                final UndeployDescription undeployDescription = UndeployDescription.of(deploymentName);
                for (String serverGroup : serverGroups) {
                    if (deployment.getServerGroups().contains(serverGroup)) {
                        matchFound = true;
                        undeployDescription.addServerGroup(serverGroup);
                    }
                }
                if (matchFound) {
                    matchedDeployments.add(undeployDescription.setFailOnMissing(failOnMissing));
                }
            }
            if (matchFound && matchPatternStrategy == MatchPatternStrategy.FIRST) {
                break;
            }
        }
    }
    if (matchPatternStrategy == MatchPatternStrategy.FAIL && matchedDeployments.size() > 1) {
        throw new MojoDeploymentException("Deployment failed, found %d deployed artifacts for pattern '%s' (%s)", matchedDeployments.size(), matchPattern, matchedDeployments);
    }
    return matchedDeployments;
}
Also used : Pattern(java.util.regex.Pattern) UndeployDescription(org.wildfly.plugin.core.UndeployDescription) TreeSet(java.util.TreeSet) DeploymentDescription(org.wildfly.plugin.core.DeploymentDescription)

Aggregations

UndeployDescription (org.wildfly.plugin.core.UndeployDescription)2 IOException (java.io.IOException)1 TreeSet (java.util.TreeSet)1 Pattern (java.util.regex.Pattern)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 MavenModelControllerClientConfiguration (org.wildfly.plugin.common.MavenModelControllerClientConfiguration)1 DeploymentDescription (org.wildfly.plugin.core.DeploymentDescription)1 DeploymentManager (org.wildfly.plugin.core.DeploymentManager)1 DeploymentResult (org.wildfly.plugin.core.DeploymentResult)1