org.kie.integration.eap.maven.EAPBaseMojo Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.integration.eap.maven;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.kie.integration.eap.maven.builder.EAPModulesDependencyBuilder;
import org.kie.integration.eap.maven.builder.EAPModulesGraphBuilder;
import org.kie.integration.eap.maven.configuration.EAPConfigurationArtifact;
import org.kie.integration.eap.maven.configuration.EAPConfigurationModuleDependency;
import org.kie.integration.eap.maven.distribution.EAPLayerDistributionManager;
import org.kie.integration.eap.maven.distribution.EAPStaticLayerDistribution;
import org.kie.integration.eap.maven.eap.EAPContainer;
import org.kie.integration.eap.maven.exception.EAPModuleDefinitionException;
import org.kie.integration.eap.maven.exception.EAPModulesDefinitionException;
import org.kie.integration.eap.maven.exception.EAPModulesDependencyBuilderException;
import org.kie.integration.eap.maven.model.dependency.EAPModuleDependency;
import org.kie.integration.eap.maven.model.dependency.EAPModuleMissingDependency;
import org.kie.integration.eap.maven.model.dependency.EAPStaticDistributionModuleDependency;
import org.kie.integration.eap.maven.model.dependency.EAPStaticModuleDependency;
import org.kie.integration.eap.maven.model.graph.EAPModulesGraph;
import org.kie.integration.eap.maven.model.layer.EAPLayer;
import org.kie.integration.eap.maven.model.module.EAPModule;
import org.kie.integration.eap.maven.model.resource.EAPModuleResource;
import org.kie.integration.eap.maven.model.resource.EAPUnresolvableArtifactResource;
import org.kie.integration.eap.maven.model.resource.EAPVersionMismatchedArtifactResource;
import org.kie.integration.eap.maven.scanner.EAPBaseModulesScanner;
import org.kie.integration.eap.maven.scanner.EAPModulesScanner;
import org.kie.integration.eap.maven.scanner.EAPStaticModulesScanner;
import org.kie.integration.eap.maven.util.EAPArtifactUtils;
import org.kie.integration.eap.maven.util.EAPArtifactsHolder;
import org.kie.integration.eap.maven.util.EAPConstants;
import org.kie.integration.eap.maven.util.EAPFileUtils;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.DependencyResolutionException;
import java.io.IOException;
import java.util.*;
public abstract class EAPBaseMojo extends AbstractMojo {
/**
* The Maven project.
*
* @parameter property="project"
* @required
* @readonly
*/
protected MavenProject project;
/**
* The entry point to Aether, i.e. the component doing all the work.
*
* @component
*/
protected RepositorySystem repoSystem;
/**
* The current repository/network configuration of Maven.
*
* @parameter default-value="${repositorySystemSession}"
* @readonly
*/
protected RepositorySystemSession repoSession;
/**
* The project's remote repositories to use for the resolution of plugins and their dependencies.
*
* @parameter default-value="${project.remoteProjectRepositories}"
* @readonly
*/
protected List remoteRepos;
/**
* The name of the JBoss EAP layer distrubtion to geneate.
*
* @parameter default-value="unnamed"
*/
protected String distributionName;
/**
* The maven module that contains all base EAP/AS modules for a given version.
*
* @parameter default-value=""
*/
protected EAPConfigurationArtifact baseModule;
/**
* The static dependencies for this distribution modules.
*
* @parameter default-value=""
*/
protected List staticDependencies;
/**
* The file to print the generated distribution graph.
*
* @parameter default-value=""
*/
protected String graphOutputFile;
/**
* The flag that indicates if the build must fail when a dependency to a module resource is not satisfied.
*
* @parameter
*/
protected Boolean failOnMissingDependency;
/**
* The flag that indicates if the build must fail when a module resource cannot be resolved in current project dependency tree.
*
* @parameter
*/
protected Boolean failOnUnresolvableResource;
/**
* The flag that indicates if the build must fail when a module version for a resource is not resolvable in current project dependencies.
*
* @parameter
*/
protected Boolean failOnVersionMismatchedResource;
/**
* The flag that indicates if the optional dependencies must be scanned in the current project dependency tree.
*
* @parameter
*/
protected Boolean includeOptionalDependencies;
/**
* The scanner for static modules.
* @component role-hint='static'
*/
protected EAPModulesScanner staticModulesScanner;
/**
* The scanner for base modules.
* @component role-hint='base'
*/
protected EAPModulesScanner baseModulesScanner;
/**
* The scanner for base modules.
* @component role-hint='default'
*/
protected EAPModulesDependencyBuilder modulesDependencyBuilder;
/**
* The scanner for base modules.
* @component role-hint='flat'
*/
protected EAPModulesGraphBuilder modulesGraphBuilder;
/**
* The scanner for base modules.
* @component role-hint='default'
*/
protected EAPLayerDistributionManager distributionManager;
/** Collection to store all resources and its modules. Usefull to check if a resource has been duplicated. **/
protected transient EAPArtifactsHolder artifactsHolder;
/**
* The static module layer.
*/
protected EAPLayer staticModulesLayer;
/**
* The base module layer.
*/
protected EAPLayer baseModulesLayer;
/**
* The target container.
*/
protected EAPContainer container = null;
/**
* The modules distribution generated.
*/
protected EAPStaticLayerDistribution distribution;
protected EAPBaseMojo() {
// Default values
failOnMissingDependency = true;
failOnUnresolvableResource = true;
includeOptionalDependencies = false;
failOnVersionMismatchedResource = false;
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// Check configuration parameters.
checkConfiguration();
// Init services.
initServices();
// Generate project's artifact.
Artifact rootArtifact = EAPArtifactUtils.createProjectArtifact(project);
DependencyNode dependencyNode = null;
try {
// Generate the dependency graph for the distribution.
dependencyNode = EAPArtifactUtils.getDependencyGraph(rootArtifact, repoSystem, repoSession, remoteRepos, includeOptionalDependencies);
} catch (DependencyCollectionException e) {
throw new MojoExecutionException("Error generating dependency graph.", e);
} catch (DependencyResolutionException e) {
throw new MojoExecutionException("Error generating dependency graph.", e);
}
// Collect the artifacts contained in the dependency graph.
artifactsHolder = collectArtifacts(dependencyNode);
// Load the static module definitions
try {
// Load the base layer modules (EAP/AS modules)
baseModulesLayer = loadBaseLayer();
// Load the static layer modules.
staticModulesLayer = loadStaticLayer();
} catch (EAPModulesDefinitionException e) {
throw new MojoExecutionException("Error obtaining modules.", e);
} catch (EAPModuleDefinitionException e) {
throw new MojoExecutionException("Error loading module.", e);
}
// Check resources.
checkResources();
try {
// Generate the modules dependencies.
generateModulesDependencies(dependencyNode);
// If exist missing dependencies, check if the build must be failed.
checkMissingDependencies();
} catch (EAPModulesDependencyBuilderException e) {
throw new MojoExecutionException("Error generating JBoss EAP modules dependencies.", e);
}
// Generates the graph.
EAPModulesGraph graph = generateModulesGraph();
// Generates the distribution.
distribution = new EAPStaticLayerDistribution(distributionName, graph, container);
distribution.setStaticLayer(staticModulesLayer);
distribution.setBaseLayer(baseModulesLayer);
distribution.setArtifactsHolder(artifactsHolder);
distribution.setIncludedOptionalDependencies(includeOptionalDependencies);
if (graphOutputFile != null && graphOutputFile.trim().length() > 0) {
try {
EAPFileUtils.writeToFile(distribution.print(), graphOutputFile);
} catch (IOException e) {
throw new MojoExecutionException("Error printing graph to file " + graphOutputFile, e);
}
}
}
protected void checkResources() throws MojoExecutionException {
// If checks are not enabled, do nothing.
if (!failOnUnresolvableResource && !failOnVersionMismatchedResource) return;
Collection