Search in sources :

Example 1 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class ModelIO method getRemotePluginVersionOverrides.

private Set<Plugin> getRemotePluginVersionOverrides(final PluginType type, final ProjectVersionRef ref, final Properties userProperties) throws ManipulationException {
    logger.debug("Resolving remote {} POM: {}", type, ref);
    final Set<Plugin> pluginOverrides = new HashSet<>();
    final Map<ProjectRef, ProjectVersionRef> pluginOverridesPomView = new HashMap<>();
    final Model m = resolveRawModel(ref);
    try {
        final MavenPomView pomView = galleyWrapper.readPomView(ref);
        final List<PluginView> deps;
        if (type == PluginType.PluginMgmt) {
            deps = pomView.getAllManagedBuildPlugins();
        } else {
            deps = pomView.getAllBuildPlugins();
        }
        for (final PluginView p : deps) {
            pluginOverridesPomView.put(p.asProjectRef(), p.asProjectVersionRef());
        }
    } catch (GalleyMavenException e) {
        throw new ManipulationException("Unable to resolve: %s", e, ref);
    }
    logger.debug("Found pluginOverridesResolvedVersions {} ", pluginOverridesPomView);
    // set of plugins with versions to handle those.
    for (Map.Entry<ProjectRef, ProjectVersionRef> entry : pluginOverridesPomView.entrySet()) {
        Plugin p = new Plugin();
        p.setArtifactId(entry.getKey().getArtifactId());
        p.setGroupId(entry.getKey().getGroupId());
        p.setVersion(entry.getValue().getVersionString());
        pluginOverrides.add(p);
    }
    // TODO: active profiles!
    if (m.getBuild() != null && m.getBuild().getPluginManagement() != null) {
        Iterator<Plugin> plit = null;
        if (type == PluginType.PluginMgmt && m.getBuild().getPluginManagement() != null) {
            logger.debug("Returning override of " + m.getBuild().getPluginManagement().getPlugins());
            plit = m.getBuild().getPluginManagement().getPlugins().iterator();
        } else if (type == PluginType.Plugins && m.getBuild().getPlugins() != null) {
            logger.debug("Returning override of " + m.getBuild().getPlugins());
            plit = m.getBuild().getPlugins().iterator();
        }
        while (plit != null && plit.hasNext()) {
            Plugin p = plit.next();
            ProjectRef pr = new SimpleProjectRef(p.getGroupId(), p.getArtifactId());
            if ((isNotEmpty(p.getVersion()) && p.getVersion().startsWith("${")) || isEmpty(p.getVersion())) {
                // Property reference to something in the remote pom. Resolve and inline it now.
                String newVersion = resolveProperty(userProperties, m.getProperties(), p.getVersion());
                // TODO: Complete replacement with PomView
                if (newVersion.startsWith("${") || newVersion.length() == 0) {
                    // Use PomView as that contains a pre-resolved list of plugins.
                    newVersion = pluginOverridesPomView.get(pr).getVersionString();
                }
                logger.debug("Replacing plugin override version " + p.getVersion() + " with " + newVersion);
                p.setVersion(newVersion);
            }
            // Replacing the element with the fully parsed element from the Model.
            pluginOverrides.remove(p);
            pluginOverrides.add(p);
            // resolve any properties.
            if (p.getConfiguration() != null) {
                processChildren(userProperties, m, (Xpp3Dom) p.getConfiguration());
            }
            if (p.getExecutions() != null) {
                List<PluginExecution> exes = p.getExecutions();
                for (PluginExecution pe : exes) {
                    if (pe.getConfiguration() != null) {
                        processChildren(userProperties, m, (Xpp3Dom) pe.getConfiguration());
                    }
                }
            }
            if (p.getDependencies() != null) {
                for (Dependency d : p.getDependencies()) {
                    if (!isEmpty(d.getVersion()) && d.getVersion().startsWith("${")) {
                        logger.debug("Processing dependency {} and updating with {} ", d, resolveProperty(userProperties, m.getProperties(), d.getVersion()));
                        d.setVersion(resolveProperty(userProperties, m.getProperties(), d.getVersion()));
                    }
                }
            }
            logger.debug("Added plugin override for {} with configuration \n" + p.getConfiguration() + " and executions " + p.getExecutions() + " and dependencies " + p.getDependencies(), p.getId());
        }
    } else {
        throw new ManipulationException("Attempting to align to a BOM that does not have a " + type.toString() + " section");
    }
    return pluginOverrides;
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Dependency(org.apache.maven.model.Dependency) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) Model(org.apache.maven.model.Model) PluginView(org.commonjava.maven.galley.maven.model.view.PluginView) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) Plugin(org.apache.maven.model.Plugin) HashSet(java.util.HashSet)

Example 2 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class ModelIO method getRemoteDependencyVersionOverrides.

public Map<ArtifactRef, String> getRemoteDependencyVersionOverrides(final ProjectVersionRef ref) throws ManipulationException {
    logger.debug("Resolving dependency management GAV: " + ref);
    final Map<ArtifactRef, String> versionOverrides = new LinkedHashMap<>();
    try {
        final MavenPomView pomView = galleyWrapper.readPomView(ref);
        // TODO: active profiles!
        final List<DependencyView> deps = pomView.getAllManagedDependencies();
        if (deps == null || deps.isEmpty()) {
            logger.warn("Attempting to align to a BOM that does not have a dependencyManagement section");
        } else {
            for (final DependencyView dep : deps) {
                versionOverrides.put(dep.asArtifactRef(), dep.getVersion());
                logger.debug("Added version override for: " + dep.asProjectRef().toString() + ":" + dep.getVersion());
            }
        }
    } catch (final GalleyMavenException e) {
        throw new ManipulationException("Unable to resolve: %s", e, ref);
    }
    return versionOverrides;
}
Also used : GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) DependencyView(org.commonjava.maven.galley.maven.model.view.DependencyView) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class SettingsIO method update.

public void update(Settings settings, File settingsFile) throws ManipulationException {
    try {
        Settings defaultSettings = new Settings();
        if (settingsFile.exists()) {
            DefaultSettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
            settingsRequest.setGlobalSettingsFile(settingsFile);
            defaultSettings = settingsBuilder.build(settingsRequest).getEffectiveSettings();
        }
        for (Profile profile : settings.getProfiles()) {
            Iterator<Profile> i = defaultSettings.getProfiles().iterator();
            while (i.hasNext()) {
                if (i.next().getId().equals(profile.getId())) {
                    i.remove();
                }
            }
            defaultSettings.addProfile(profile);
        }
        for (String activeProfile : settings.getActiveProfiles()) {
            Iterator<String> i = defaultSettings.getActiveProfiles().iterator();
            while (i.hasNext()) {
                if (i.next().equals(activeProfile)) {
                    i.remove();
                }
            }
            defaultSettings.addActiveProfile(activeProfile);
        }
        for (Mirror mirror : settings.getMirrors()) {
            defaultSettings.addMirror(mirror);
        }
        for (Proxy proxy : settings.getProxies()) {
            defaultSettings.addProxy(proxy);
        }
        for (Server server : settings.getServers()) {
            defaultSettings.addServer(server);
        }
        for (String pluginGroup : settings.getPluginGroups()) {
            defaultSettings.addPluginGroup(pluginGroup);
        }
        if (settings.getLocalRepository() != null) {
            defaultSettings.setLocalRepository(settings.getLocalRepository());
        }
        write(defaultSettings, settingsFile);
    } catch (SettingsBuildingException e) {
        throw new ManipulationException("Failed to build existing settings.xml for repo removal backup.", e, settingsFile, e.getMessage());
    }
}
Also used : SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) Proxy(org.apache.maven.settings.Proxy) Server(org.apache.maven.settings.Server) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) Mirror(org.apache.maven.settings.Mirror) Settings(org.apache.maven.settings.Settings) Profile(org.apache.maven.settings.Profile)

Example 4 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class GalleyInfrastructure method init.

private void init(final File targetDirectory, final List<ArtifactRepository> remoteRepositories, final ArtifactRepository localRepository, final Settings settings, final List<String> activeProfiles, final Location customLocation, final Transport customTransport, File cacheDir_) throws ManipulationException {
    LocationExpander locationExpander;
    try {
        final List<Location> custom = customLocation == null ? Collections.<Location>emptyList() : Collections.singletonList(customLocation);
        locationExpander = new MavenLocationExpander(custom, remoteRepositories, localRepository, mirrorSelector, settings, activeProfiles);
    } catch (final MalformedURLException e) {
        throw new ManipulationException("Failed to setup Maven-specific LocationExpander: %s", e, e.getMessage());
    }
    xml = new XMLInfrastructure();
    xpaths = new XPathManager();
    final TransportManager transports;
    if (customTransport != null) {
        transports = new TransportManagerImpl(customTransport);
    } else {
        transports = new TransportManagerImpl(new HttpClientTransport(new HttpImpl(new MemoryPasswordManager())), new FileTransport(), new ZipJarTransport());
    }
    cacheDir = cacheDir_;
    if (cacheDir == null) {
        cacheDir = new File(targetDirectory, "manipulator-cache");
    }
    final FileEventManager fileEvents = new NoOpFileEventManager();
    final CacheProvider cache = new FileCacheProvider(cacheDir, new HashedLocationPathGenerator(), fileEvents, new NoOpTransferDecorator());
    final NotFoundCache nfc = new MemoryNotFoundCache();
    executor = Executors.newCachedThreadPool();
    final TransportManagerConfig config = new TransportManagerConfig();
    final TransferManager transfers = new TransferManagerImpl(transports, cache, nfc, fileEvents, new DownloadHandler(nfc, config, executor), new UploadHandler(nfc, config, executor), new ListingHandler(nfc), new ExistenceHandler(nfc), new SpecialPathManagerImpl(), executor);
    final TypeMapper types = new StandardTypeMapper();
    final ArtifactMetadataManager metadataManager = new ArtifactMetadataManagerImpl(transfers, locationExpander);
    final VersionResolver versionResolver = new VersionResolverImpl(new MavenMetadataReader(xml, locationExpander, metadataManager, xpaths));
    artifactManager = new ArtifactManagerImpl(transfers, locationExpander, types, versionResolver);
    // TODO: auto-adjust this to the current Maven runtime!
    final MavenPluginDefaults pluginDefaults = new StandardMaven304PluginDefaults();
    final MavenPluginImplications pluginImplications = new StandardMavenPluginImplications(xml);
    pomReader = new MavenPomReader(xml, locationExpander, artifactManager, xpaths, pluginDefaults, pluginImplications);
    metadataReader = new MavenMetadataReader(xml, locationExpander, metadataManager, xpaths);
}
Also used : StandardTypeMapper(org.commonjava.maven.galley.maven.internal.type.StandardTypeMapper) TypeMapper(org.commonjava.maven.galley.maven.spi.type.TypeMapper) MalformedURLException(java.net.MalformedURLException) NoOpTransferDecorator(org.commonjava.maven.galley.io.NoOpTransferDecorator) TransferManagerImpl(org.commonjava.maven.galley.internal.TransferManagerImpl) VersionResolverImpl(org.commonjava.maven.galley.maven.internal.version.VersionResolverImpl) MavenPluginDefaults(org.commonjava.maven.galley.maven.spi.defaults.MavenPluginDefaults) XPathManager(org.commonjava.maven.galley.maven.model.view.XPathManager) HttpImpl(org.commonjava.maven.galley.transport.htcli.HttpImpl) SpecialPathManagerImpl(org.commonjava.maven.galley.io.SpecialPathManagerImpl) LocationExpander(org.commonjava.maven.galley.spi.transport.LocationExpander) StandardMavenPluginImplications(org.commonjava.maven.galley.maven.internal.defaults.StandardMavenPluginImplications) MavenPluginImplications(org.commonjava.maven.galley.maven.spi.defaults.MavenPluginImplications) ArtifactMetadataManager(org.commonjava.maven.galley.maven.ArtifactMetadataManager) MavenMetadataReader(org.commonjava.maven.galley.maven.parse.MavenMetadataReader) MemoryNotFoundCache(org.commonjava.maven.galley.nfc.MemoryNotFoundCache) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) UploadHandler(org.commonjava.maven.galley.internal.xfer.UploadHandler) StandardTypeMapper(org.commonjava.maven.galley.maven.internal.type.StandardTypeMapper) ExistenceHandler(org.commonjava.maven.galley.internal.xfer.ExistenceHandler) FileCacheProvider(org.commonjava.maven.galley.cache.FileCacheProvider) TransferManager(org.commonjava.maven.galley.TransferManager) ListingHandler(org.commonjava.maven.galley.internal.xfer.ListingHandler) TransportManagerImpl(org.commonjava.maven.galley.transport.TransportManagerImpl) HashedLocationPathGenerator(org.commonjava.maven.galley.io.HashedLocationPathGenerator) DownloadHandler(org.commonjava.maven.galley.internal.xfer.DownloadHandler) TransportManagerConfig(org.commonjava.maven.galley.config.TransportManagerConfig) MemoryNotFoundCache(org.commonjava.maven.galley.nfc.MemoryNotFoundCache) MemoryPasswordManager(org.commonjava.maven.galley.auth.MemoryPasswordManager) CacheProvider(org.commonjava.maven.galley.spi.cache.CacheProvider) FileCacheProvider(org.commonjava.maven.galley.cache.FileCacheProvider) StandardMavenPluginImplications(org.commonjava.maven.galley.maven.internal.defaults.StandardMavenPluginImplications) ArtifactMetadataManagerImpl(org.commonjava.maven.galley.maven.internal.ArtifactMetadataManagerImpl) NoOpFileEventManager(org.commonjava.maven.galley.event.NoOpFileEventManager) StandardMaven304PluginDefaults(org.commonjava.maven.galley.maven.internal.defaults.StandardMaven304PluginDefaults) HttpClientTransport(org.commonjava.maven.galley.transport.htcli.HttpClientTransport) NoOpFileEventManager(org.commonjava.maven.galley.event.NoOpFileEventManager) FileEventManager(org.commonjava.maven.galley.spi.event.FileEventManager) VersionResolver(org.commonjava.maven.galley.maven.spi.version.VersionResolver) FileTransport(org.commonjava.maven.galley.filearc.FileTransport) ZipJarTransport(org.commonjava.maven.galley.filearc.ZipJarTransport) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) XMLInfrastructure(org.commonjava.maven.galley.maven.parse.XMLInfrastructure) ArtifactManagerImpl(org.commonjava.maven.galley.maven.internal.ArtifactManagerImpl) TransportManager(org.commonjava.maven.galley.spi.transport.TransportManager) MavenPomReader(org.commonjava.maven.galley.maven.parse.MavenPomReader) File(java.io.File) Location(org.commonjava.maven.galley.model.Location)

Example 5 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class PomIO method write.

private void write(final Project project, final File pom, final Model model) throws ManipulationException {
    try {
        final String manifestInformation = project.isInheritanceRoot() ? ManifestUtils.getManifestInformation() : null;
        MavenJDOMWriter mjw = new MavenJDOMWriter(model);
        // We possibly could store the EOL type in the Project when we first read
        // the file but we would then have to do a dual read, then write as opposed
        // to a read, then read + write now.
        LineSeparator ls = determineEOL(pom);
        mjw.setLineSeparator(ls);
        mjw.write(model, pom, new DocumentModifier() {

            @Override
            public void postProcess(final Document doc) {
                // Only add the modified by to the top level pom.
                if (project.isExecutionRoot()) {
                    final Iterator<Content> it = doc.getContent(new ContentFilter(ContentFilter.COMMENT)).iterator();
                    while (it.hasNext()) {
                        final Comment c = (Comment) it.next();
                        if (c.toString().contains(MODIFIED_BY)) {
                            it.remove();
                        }
                    }
                    doc.addContent(Collections.<Content>singletonList(new Comment("\nModified by POM Manipulation Extension for Maven " + manifestInformation + "\n")));
                }
            }
        });
    } catch (final IOException e) {
        throw new ManipulationException("Failed to read POM for rewrite: %s. Reason: %s", e, pom, e.getMessage());
    } catch (final JDOMException e) {
        throw new ManipulationException("Failed to parse POM for rewrite: %s. Reason: %s", e, pom, e.getMessage());
    }
}
Also used : Comment(org.jdom2.Comment) IOException(java.io.IOException) MavenJDOMWriter(org.apache.maven.model.io.jdom.MavenJDOMWriter) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) LineSeparator(org.jdom2.output.LineSeparator) ContentFilter(org.jdom2.filter.ContentFilter) DocumentModifier(org.apache.maven.io.util.DocumentModifier) Content(org.jdom2.Content) Iterator(java.util.Iterator) ManipulationException(org.commonjava.maven.ext.common.ManipulationException)

Aggregations

ManipulationException (org.commonjava.maven.ext.common.ManipulationException)42 IOException (java.io.IOException)13 File (java.io.File)11 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 LinkedHashMap (java.util.LinkedHashMap)7 Project (org.commonjava.maven.ext.common.model.Project)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)6 Properties (java.util.Properties)5 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)5 JsonPathException (com.jayway.jsonpath.JsonPathException)4 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)4 SimpleProjectRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectRef)4 CommonState (org.commonjava.maven.ext.core.state.CommonState)4 Test (org.junit.Test)4 DocumentContext (com.jayway.jsonpath.DocumentContext)3 FileInputStream (java.io.FileInputStream)3 Dependency (org.apache.maven.model.Dependency)3