Search in sources :

Example 1 with Artifact

use of io.fabric8.patch.management.Artifact in project fabric8 by jboss-fuse.

the class AetherBasedResolver method collectDependencies.

protected DependencyNode collectDependencies(Artifact root, String pomVersion, final Filter<Dependency> excludeDependencyFilter) throws RepositoryException, IOException {
    final DefaultRepositorySystemSession session = newSession();
    try {
        List<RemoteRepository> repos = selectRepositories();
        assignProxyAndMirrors(repos);
        ArtifactDescriptorResult artifactDescriptorResult = m_repoSystem.readArtifactDescriptor(session, new ArtifactDescriptorRequest(root, repos, null));
        repos.addAll(artifactDescriptorResult.getRepositories());
        Dependency rootDependency = new Dependency(root, null);
        List<Dependency> dependencies = artifactDescriptorResult.getDependencies();
        final DefaultDependencyNode rootNode = new DefaultDependencyNode(rootDependency);
        GenericVersionScheme versionScheme = new GenericVersionScheme();
        rootNode.setVersion(versionScheme.parseVersion(pomVersion));
        rootNode.setVersionConstraint(versionScheme.parseVersionConstraint(pomVersion));
        DependencyNode pomNode = rootNode;
        // final Filter<Dependency> shouldExclude = Filters.or(DependencyFilters.testScopeFilter, excludeDependencyFilter, new NewerVersionExistsFilter(rootNode));
        final Filter<Dependency> shouldExclude = Filters.or(Arrays.asList(DependencyFilters.testScopeFilter, excludeDependencyFilter));
        DependencySelector dependencySelector = new AndDependencySelector(new ScopeDependencySelector("test"), new ExclusionDependencySelector(), new DependencySelector() {

            @Override
            public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
                return this;
            }

            @Override
            public boolean selectDependency(Dependency dependency) {
                try {
                    return !DependencyFilters.matches(dependency, shouldExclude);
                } catch (Exception e) {
                    failedToMakeDependencyTree(dependency, e);
                    return false;
                }
            }
        });
        session.setDependencySelector(dependencySelector);
        // work on the root dependency directly?
        if (true) {
            for (Dependency dependency : dependencies) {
                DependencyNode node = resolveDependencies(session, repos, pomNode, dependency, shouldExclude);
                if (node != null) {
                    pomNode.getChildren().add(node);
                }
            }
        } else {
            DependencyNode node = resolveDependencies(session, repos, pomNode, rootDependency, shouldExclude);
            if (node != null) {
                pomNode = node;
            }
        }
        // now lets transform the dependency tree to remove different versions for the same artifact
        final DependencyGraphTransformationContext tranformContext = new DependencyGraphTransformationContext() {

            Map<Object, Object> map = new HashMap<>();

            public RepositorySystemSession getSession() {
                return session;
            }

            public Object get(Object key) {
                return map.get(key);
            }

            public Object put(Object key, Object value) {
                return map.put(key, value);
            }
        };
        DependencyGraphTransformer transformer = new ReplaceConflictingVersionResolver();
        pomNode = transformer.transformGraph(pomNode, tranformContext);
        transformer = new DuplicateTransformer();
        pomNode = transformer.transformGraph(pomNode, tranformContext);
        return pomNode;
    } finally {
        releaseSession(session);
    }
}
Also used : DependencyGraphTransformationContext(org.eclipse.aether.collection.DependencyGraphTransformationContext) ReplaceConflictingVersionResolver(io.fabric8.maven.ReplaceConflictingVersionResolver) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DuplicateTransformer(io.fabric8.maven.DuplicateTransformer) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DependencyNode(org.eclipse.aether.graph.DependencyNode) ChainedDependencyGraphTransformer(org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer) DependencyGraphTransformer(org.eclipse.aether.collection.DependencyGraphTransformer) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) ScopeDependencySelector(org.eclipse.aether.util.graph.selector.ScopeDependencySelector) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) DependencyCollectionContext(org.eclipse.aether.collection.DependencyCollectionContext) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) Dependency(org.eclipse.aether.graph.Dependency) FailedToResolveDependency(io.fabric8.maven.FailedToResolveDependency) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) RepositoryException(org.eclipse.aether.RepositoryException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) MetadataTransferException(org.eclipse.aether.transfer.MetadataTransferException) NoRouteToHostException(java.net.NoRouteToHostException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) MalformedURLException(java.net.MalformedURLException) MetadataNotFoundException(org.eclipse.aether.transfer.MetadataNotFoundException) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) ScopeDependencySelector(org.eclipse.aether.util.graph.selector.ScopeDependencySelector) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) DependencySelector(org.eclipse.aether.collection.DependencySelector) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 2 with Artifact

use of io.fabric8.patch.management.Artifact in project fabric8 by jboss-fuse.

the class ProjectDeployerImpl method resolveProfileDeployments.

private DeployResults resolveProfileDeployments(ProjectRequirements requirements, FabricService fabric, Profile profile, ProfileBuilder builder, boolean mergeWithOldProfile) throws Exception {
    DependencyDTO rootDependency = requirements.getRootDependency();
    ProfileService profileService = fabricService.get().adapt(ProfileService.class);
    if (!mergeWithOldProfile) {
        // If we're not merging with the old profile, then create a dummy empty profile to merge with instead.
        ProfileBuilder emptyProfile = ProfileBuilder.Factory.create(profile.getVersion(), profile.getId()).setOverlay(true);
        profile = emptyProfile.getProfile();
    }
    if (rootDependency != null) {
        // as a hack lets just add this bundle in
        LOG.info("Got root: " + rootDependency);
        List<String> parentIds = profile.getParentIds();
        Profile overlay = profileService.getOverlayProfile(profile);
        String bundleUrl = rootDependency.toBundleUrlWithType();
        LOG.info("Using resolver to add extra features and bundles on " + bundleUrl);
        List<String> features = new ArrayList<String>(profile.getFeatures());
        List<String> bundles = new ArrayList<String>(profile.getBundles());
        List<String> optionals = new ArrayList<String>(profile.getOptionals());
        if (requirements.getFeatures() != null) {
            features.addAll(requirements.getFeatures());
        }
        if (requirements.getBundles() != null) {
            bundles.addAll(requirements.getBundles());
        }
        bundles.add(bundleUrl);
        LOG.info("Adding bundle: " + bundleUrl);
        // TODO we maybe should detect a karaf based container in a nicer way than this?
        boolean isKarafContainer = parentIds.contains("karaf") || parentIds.contains("containers-karaf");
        boolean addBundleDependencies = Objects.equal("bundle", rootDependency.getType()) || isKarafContainer;
        if (addBundleDependencies && requirements.isUseResolver()) {
            // lets build up a list of all current active features and bundles along with all discovered features
            List<Feature> availableFeatures = new ArrayList<Feature>();
            addAvailableFeaturesFromProfile(availableFeatures, fabric, overlay);
            Set<String> currentBundleLocations = new HashSet<>();
            currentBundleLocations.addAll(bundles);
            // lets add the current features
            DownloadManager downloadManager = DownloadManagers.createDownloadManager(fabric, executorService);
            Set<Feature> currentFeatures = AgentUtils.getFeatures(fabric, downloadManager, overlay);
            addBundlesFromProfile(currentBundleLocations, overlay);
            List<String> parentProfileIds = requirements.getParentProfiles();
            if (parentProfileIds != null) {
                for (String parentProfileId : parentProfileIds) {
                    Profile parentProfile = profileService.getProfile(profile.getVersion(), parentProfileId);
                    Profile parentOverlay = profileService.getOverlayProfile(parentProfile);
                    Set<Feature> parentFeatures = AgentUtils.getFeatures(fabric, downloadManager, parentOverlay);
                    currentFeatures.addAll(parentFeatures);
                    addAvailableFeaturesFromProfile(availableFeatures, fabric, parentOverlay);
                    addBundlesFromProfile(currentBundleLocations, parentOverlay);
                }
            }
            // lets add all known features from the known repositories
            for (DependencyDTO dependency : rootDependency.getChildren()) {
                if ("test".equals(dependency.getScope()) || "provided".equals(dependency.getScope())) {
                    continue;
                }
                if ("jar".equals(dependency.getType())) {
                    String match = getAllServiceMixBundles().get(dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion());
                    if (match != null) {
                        LOG.info("Replacing artifact " + dependency + " with servicemix bundle " + match);
                        String[] parts = match.split(":");
                        dependency.setGroupId(parts[0]);
                        dependency.setArtifactId(parts[1]);
                        dependency.setVersion(parts[2]);
                        dependency.setType("bundle");
                    }
                }
                String prefix = dependency.toBundleUrlWithoutVersion();
                Feature feature = findFeatureWithBundleLocationPrefix(currentFeatures, prefix);
                if (feature != null) {
                    LOG.info("Feature is already is in the profile " + feature.getId() + " for " + dependency.toBundleUrl());
                } else {
                    feature = findFeatureWithBundleLocationPrefix(availableFeatures, prefix);
                    if (feature != null) {
                        String name = feature.getName();
                        if (features.contains(name)) {
                            LOG.info("Feature is already added " + name + " for " + dependency.toBundleUrl());
                        } else {
                            LOG.info("Found a matching feature for bundle " + dependency.toBundleUrl() + ": " + feature.getId());
                            features.add(name);
                        }
                    } else {
                        String bundleUrlWithType = dependency.toBundleUrlWithType();
                        String foundBundleUri = findBundleUri(currentBundleLocations, prefix);
                        if (foundBundleUri != null) {
                            LOG.info("Bundle already included " + foundBundleUri + " for " + bundleUrlWithType);
                        } else {
                            boolean ignore = false;
                            String bundleWithoutMvnPrefix = getMavenCoords(bundleUrlWithType);
                            for (String ignoreBundlePrefix : RESOLVER_IGNORE_BUNDLE_PREFIXES) {
                                if (bundleWithoutMvnPrefix.startsWith(ignoreBundlePrefix)) {
                                    ignore = true;
                                    break;
                                }
                            }
                            if (ignore) {
                                LOG.info("Ignoring bundle: " + bundleUrlWithType);
                            } else {
                                boolean optional = dependency.isOptional();
                                LOG.info("Adding " + (optional ? "optional " : "") + " bundle: " + bundleUrlWithType);
                                if (optional) {
                                    optionals.add(bundleUrlWithType);
                                } else {
                                    bundles.add(bundleUrlWithType);
                                }
                            }
                        }
                    }
                }
            }
            // Modify the profile through the {@link ProfileBuilder}
            builder.setOptionals(optionals).setFeatures(features);
        }
        builder.setBundles(bundles);
    }
    profile = profileService.updateProfile(builder.getProfile());
    Integer minimumInstances = requirements.getMinimumInstances();
    if (minimumInstances != null) {
        FabricRequirements fabricRequirements = fabricService.get().getRequirements();
        ProfileRequirements profileRequirements = fabricRequirements.getOrCreateProfileRequirement(profile.getId());
        profileRequirements.setMinimumInstances(minimumInstances);
        fabricRequirements.setVersion(requirements.getVersion());
        fabricService.get().setRequirements(fabricRequirements);
    }
    // lets find a hawtio profile and version
    String profileUrl = findHawtioUrl(fabric);
    if (profileUrl == null) {
        profileUrl = "/";
    }
    if (!profileUrl.endsWith("/")) {
        profileUrl += "/";
    }
    String profilePath = Profiles.convertProfileIdToPath(profile.getId());
    profileUrl += "index.html#/wiki/branch/" + profile.getVersion() + "/view/fabric/profiles/" + profilePath;
    return new DeployResults(profile, profileUrl);
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) DeployResults(io.fabric8.deployer.dto.DeployResults) ArrayList(java.util.ArrayList) DependencyDTO(io.fabric8.deployer.dto.DependencyDTO) ProfileBuilder(io.fabric8.api.ProfileBuilder) Feature(io.fabric8.agent.model.Feature) DownloadManager(io.fabric8.agent.download.DownloadManager) Profile(io.fabric8.api.Profile) ProfileService(io.fabric8.api.ProfileService) FabricRequirements(io.fabric8.api.FabricRequirements) HashSet(java.util.HashSet)

Example 3 with Artifact

use of io.fabric8.patch.management.Artifact in project fabric8 by jboss-fuse.

the class OpenShiftPomDeployer method updateDependencyPlugin.

/**
 * Lets add/update the maven dependency plugin configuration to copy deployments
 * to the deployDir or the webAppDir
 */
protected void updateDependencyPlugin(Element plugins, Element dependencies, Collection<Parser> artifacts) throws XPathExpressionException {
    Element plugin = getOrCreatePlugin(plugins, "maven-dependency-plugin", "2.8");
    Element executions = getOrCreateChild(plugin, "executions", 6);
    List<Parser> warArtifacts = new ArrayList<Parser>();
    List<Parser> jarArtifacts = new ArrayList<Parser>();
    for (Parser artifact : artifacts) {
        String type = artifact.getType();
        if (Objects.equal("war", type)) {
            warArtifacts.add(artifact);
        } else {
            jarArtifacts.add(artifact);
        }
    }
    if (Strings.isNotBlank(webAppDir) && !warArtifacts.isEmpty()) {
        recreateDependencyExecution(executions, dependencies, "fuse-fabric-deploy-webapps", webAppDir, warArtifacts, true);
    }
    if (Strings.isNotBlank(deployDir) && !jarArtifacts.isEmpty()) {
        recreateDependencyExecution(executions, dependencies, "fuse-fabric-deploy-shared", deployDir, jarArtifacts, false);
    }
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Parser(io.fabric8.maven.util.Parser)

Example 4 with Artifact

use of io.fabric8.patch.management.Artifact in project fabric8 by jboss-fuse.

the class OpenShiftPomDeployerTest method doTest.

protected void doTest(String folder, String[] artifactUrls, String[] repoUrls, String expectedCamelDependencyScope, String expectedHawtioDependencyScope) throws Exception {
    File sourceDir = new File(baseDir, "src/test/resources/" + folder);
    assertDirectoryExists(sourceDir);
    File pomSource = new File(sourceDir, "pom.xml");
    assertFileExists(pomSource);
    File outputDir = new File(baseDir, "target/" + getClass().getName() + "/" + folder);
    outputDir.mkdirs();
    assertDirectoryExists(outputDir);
    File pom = new File(outputDir, "pom.xml");
    Files.copy(pomSource, pom);
    assertFileExists(pom);
    git = Git.init().setDirectory(outputDir).setGitDir(new File(outputDir, ".git")).call();
    assertDirectoryExists(new File(outputDir, ".git"));
    git.add().addFilepattern("pom.xml").call();
    git.commit().setMessage("Initial import").call();
    // now we have the git repo setup; lets run the update
    OpenShiftPomDeployer deployer = new OpenShiftPomDeployer(git, outputDir, deployDir, webAppDir);
    System.out.println("About to update the pom " + pom + " with artifacts: " + Arrays.asList(artifactUrls));
    List<Parser> artifacts = new ArrayList<Parser>();
    for (String artifactUrl : artifactUrls) {
        artifacts.add(Parser.parsePathWithSchemePrefix(artifactUrl));
    }
    List<MavenRepositoryURL> repos = new ArrayList<MavenRepositoryURL>();
    for (String repoUrl : repoUrls) {
        repos.add(new MavenRepositoryURL(repoUrl));
    }
    deployer.update(artifacts, repos);
    System.out.println("Completed the new pom is: ");
    System.out.println(Files.toString(pom));
    Document xml = XmlUtils.parseDoc(pom);
    Element plugins = assertXPathElement(xml, "project/profiles/profile[id = 'openshift']/build/plugins");
    Element cleanExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-clean-plugin']/executions/execution[id = 'fuse-fabric-clean']");
    Element dependencySharedExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-dependency-plugin']/executions/execution[id = 'fuse-fabric-deploy-shared']");
    Element dependencyWebAppsExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-dependency-plugin']/executions/execution[id = 'fuse-fabric-deploy-webapps']");
    Element warPluginWarName = xpath("plugin[artifactId = 'maven-war-plugin']/configuration/warName").element(plugins);
    if (warPluginWarName != null) {
        String warName = warPluginWarName.getTextContent();
        System.out.println("WarName is now:  " + warName);
        assertTrue("Should not have ROOT war name", !"ROOT".equals(warName));
    }
    Element dependencies = assertXPathElement(xml, "project/dependencies");
    Element repositories = assertXPathElement(xml, "project/repositories");
    for (Parser artifact : artifacts) {
        // lets check there's only 1 dependency for group & artifact and it has the right version
        String group = groupId(artifact);
        String artifactId = artifact.getArtifact();
        Element dependency = assertSingleDependencyForGroupAndArtifact(dependencies, group, artifactId);
        Element version = assertXPathElement(dependency, "version");
        assertEquals("Version", artifact.getVersion(), version.getTextContent());
    }
    // lets check we either preserve scope, add provided or don't add a scope if there's none present in the underlying pom
    assertDependencyScope(dependencies, "org.apache.camel", "camel-core", expectedCamelDependencyScope);
    assertDependencyScope(dependencies, "org.drools", "drools-wb-distribution-wars", "provided");
    assertDependencyScope(dependencies, "io.hawt", "hawtio-web", expectedHawtioDependencyScope);
    assertRepositoryUrl(repositories, "https://maven.repository.redhat.com/ga/");
    assertRepositoryUrl(repositories, "https://repo.fusesource.com/nexus/content/groups/ea/");
}
Also used : OpenShiftPomDeployer(io.fabric8.openshift.agent.OpenShiftPomDeployer) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MavenRepositoryURL(io.fabric8.maven.util.MavenRepositoryURL) Document(org.w3c.dom.Document) File(java.io.File) Parser(io.fabric8.maven.util.Parser)

Example 5 with Artifact

use of io.fabric8.patch.management.Artifact in project fabric8 by jboss-fuse.

the class MavenUploadProxyServlet method handleDeploy.

private void handleDeploy(HttpServletRequest req, UploadContext result) throws Exception {
    String profile = req.getParameter("profile");
    String version = req.getParameter("version");
    if (profile != null && version != null) {
        ProjectRequirements requirements = toProjectRequirements(result);
        requirements.setProfileId(profile);
        requirements.setVersion(version);
        DeployResults deployResults = addToProfile(requirements);
        LOGGER.info(String.format("Deployed artifact %s to profile: %s", result.toArtifact(), deployResults));
    }
}
Also used : DeployResults(io.fabric8.deployer.dto.DeployResults) ProjectRequirements(io.fabric8.deployer.dto.ProjectRequirements)

Aggregations

File (java.io.File)22 Artifact (io.fabric8.patch.management.Artifact)11 Test (org.junit.Test)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)8 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)8 FileInputStream (java.io.FileInputStream)6 MavenResolver (io.fabric8.maven.MavenResolver)5 PatchException (io.fabric8.patch.management.PatchException)5 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)5 FileOutputStream (java.io.FileOutputStream)4 MalformedURLException (java.net.MalformedURLException)4 Git (org.eclipse.jgit.api.Git)4 Version (org.osgi.framework.Version)4 DeployResults (io.fabric8.deployer.dto.DeployResults)3 Artifact.isSameButVersion (io.fabric8.patch.management.Artifact.isSameButVersion)3 Patch (io.fabric8.patch.management.Patch)3 Utils.mvnurlToArtifact (io.fabric8.patch.management.Utils.mvnurlToArtifact)3 HashSet (java.util.HashSet)3