Search in sources :

Example 1 with BOMInjectingState

use of org.commonjava.maven.ext.core.state.BOMInjectingState in project pom-manipulation-ext by release-engineering.

the class BOMBuilderManipulator method applyChanges.

/**
 * If enabled, grab the execution root pom (which will be the topmost POM in terms of directory structure). Within that
 * handle the manipulation of the bom injection.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final BOMInjectingState state = session.getState(BOMInjectingState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    List<Dependency> projectArtifacts = getArtifacts(projects);
    for (final Project project : projects) {
        if (project.isExecutionRoot()) {
            logger.info("Examining {} to add BOM generation.", project);
            final Model model = project.getModel();
            Build build = model.getBuild();
            if (build == null) {
                build = new Build();
                model.setBuild(build);
            }
            Model bomModel = createModel(project, IDBOM);
            bomModel.setDescription("PME Generated BOM for other projects to use to align to.");
            DependencyManagement dm = new DependencyManagement();
            dm.setDependencies(projectArtifacts);
            bomModel.setDependencyManagement(dm);
            // Write new bom back out.
            File pmebom = new File(session.getTargetDir(), IDBOM + ".xml");
            session.getTargetDir().mkdir();
            pomIO.writeModel(bomModel, pmebom);
            final Map<String, Plugin> pluginMap = build.getPluginsAsMap();
            if (!pluginMap.containsKey(POM_DEPLOYER_COORD)) {
                final PluginExecution execution = new PluginExecution();
                execution.setId(IDBOM);
                execution.setPhase("install");
                execution.setGoals(Collections.singletonList("add-pom"));
                final Plugin plugin = new Plugin();
                plugin.setGroupId(POM_DEPLOYER_GID);
                plugin.setArtifactId(POM_DEPLOYER_AID);
                plugin.setVersion(POM_DEPLOYER_VID);
                plugin.addExecution(execution);
                plugin.setInherited(false);
                build.addPlugin(plugin);
                final Xpp3Dom xml = new Xpp3Dom("configuration");
                final Map<String, Object> config = new HashMap<>();
                config.put("pomName", "target" + File.separatorChar + pmebom.getName());
                config.put("errorOnMissing", false);
                config.put("artifactId", bomModel.getArtifactId());
                config.put("groupId", bomModel.getGroupId());
                for (final Map.Entry<String, Object> entry : config.entrySet()) {
                    final Xpp3Dom child = new Xpp3Dom(entry.getKey());
                    if (entry.getValue() != null) {
                        child.setValue(entry.getValue().toString());
                    }
                    xml.addChild(child);
                }
                execution.setConfiguration(xml);
            }
            return Collections.singleton(project);
        }
    }
    return Collections.emptySet();
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) HashMap(java.util.HashMap) Dependency(org.apache.maven.model.Dependency) Project(org.commonjava.maven.ext.common.model.Project) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) BOMInjectingState(org.commonjava.maven.ext.core.state.BOMInjectingState) DependencyManagement(org.apache.maven.model.DependencyManagement) Plugin(org.apache.maven.model.Plugin)

Example 2 with BOMInjectingState

use of org.commonjava.maven.ext.core.state.BOMInjectingState in project pom-manipulation-ext by release-engineering.

the class BOMBuilderManipulator method init.

@Override
public void init(final ManipulationSession session) throws ManipulationException {
    this.session = session;
    session.setState(new BOMInjectingState(session.getUserProperties()));
}
Also used : BOMInjectingState(org.commonjava.maven.ext.core.state.BOMInjectingState)

Aggregations

BOMInjectingState (org.commonjava.maven.ext.core.state.BOMInjectingState)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Build (org.apache.maven.model.Build)1 Dependency (org.apache.maven.model.Dependency)1 DependencyManagement (org.apache.maven.model.DependencyManagement)1 Model (org.apache.maven.model.Model)1 Plugin (org.apache.maven.model.Plugin)1 PluginExecution (org.apache.maven.model.PluginExecution)1 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)1 Project (org.commonjava.maven.ext.common.model.Project)1