Search in sources :

Example 71 with Dependency

use of org.apache.maven.model.Dependency in project maven-plugins by apache.

the class WebappStructureTest method testDependencyAnalysisWithNewDependency.

public void testDependencyAnalysisWithNewDependency() {
    final List<Dependency> dependencies = new ArrayList<Dependency>();
    dependencies.add(createDependency("groupTest", "artifactTest", "1.0"));
    final WebappStructure cache = new WebappStructure(dependencies);
    final List<Dependency> newDependencies = new ArrayList<Dependency>(dependencies);
    final Dependency newDependency = createDependency("groupTest", "nexArtifact", "2.0");
    newDependencies.add(newDependency);
    final WebappStructure webappStructure = new WebappStructure(newDependencies, cache);
    webappStructure.analyseDependencies(new WebappStructure.DependenciesAnalysisCallback() {

        int count = 0;

        public void unchangedDependency(Dependency dependency) {
            if (count == 0) {
                count++;
            } else {
                fail("Should have called unchanged dependency only once");
            }
        }

        public void newDependency(Dependency dependency) {
            if (!newDependency.equals(dependency)) {
                fail("Called new dependency with an unexpected dependency " + dependency);
            }
        }

        public void removedDependency(Dependency dependency) {
            fail("Should have failed to trigger this callback");
        }

        public void updatedVersion(Dependency dependency, String previousVersion) {
            fail("Should have failed to trigger this callback");
        }

        public void updatedScope(Dependency dependency, String previousScope) {
            fail("Should have failed to trigger this callback");
        }

        public void updatedOptionalFlag(Dependency dependency, boolean previousOptional) {
            fail("Should have failed to trigger this callback");
        }

        public void updatedUnknown(Dependency dependency, Dependency previousDep) {
            fail("Should have failed to trigger this callback");
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) WebappStructure(org.apache.maven.plugins.war.util.WebappStructure)

Example 72 with Dependency

use of org.apache.maven.model.Dependency in project sling by apache.

the class ModelPreprocessor method searchSlingstartDependencies.

/**
     * Search for dependent slingstart/slingfeature artifacts and remove them from the effective model.
     * @throws MavenExecutionException
     */
private List<Model> searchSlingstartDependencies(final Environment env, final ProjectInfo info, final Model rawModel, final Model effectiveModel) throws MavenExecutionException {
    // slingstart or slingfeature
    final List<Model> dependencies = new ArrayList<>();
    for (final Feature feature : effectiveModel.getFeatures()) {
        for (final RunMode runMode : feature.getRunModes()) {
            for (final ArtifactGroup group : runMode.getArtifactGroups()) {
                final List<org.apache.sling.provisioning.model.Artifact> removeList = new ArrayList<>();
                for (final org.apache.sling.provisioning.model.Artifact a : group) {
                    if (a.getType().equals(BuildConstants.PACKAGING_SLINGSTART) || a.getType().equals(BuildConstants.PACKAGING_PARTIAL_SYSTEM)) {
                        final Dependency dep = new Dependency();
                        dep.setGroupId(a.getGroupId());
                        dep.setArtifactId(a.getArtifactId());
                        dep.setVersion(a.getVersion());
                        dep.setType(BuildConstants.PACKAGING_PARTIAL_SYSTEM);
                        if (a.getType().equals(BuildConstants.PACKAGING_SLINGSTART)) {
                            dep.setClassifier(BuildConstants.PACKAGING_PARTIAL_SYSTEM);
                        } else {
                            dep.setClassifier(a.getClassifier());
                        }
                        dep.setScope(Artifact.SCOPE_PROVIDED);
                        env.logger.debug("- adding dependency " + ModelUtils.toString(dep));
                        info.project.getDependencies().add(dep);
                        // if it's a project from the current reactor build, we can't resolve it right now
                        final String key = a.getGroupId() + ":" + a.getArtifactId();
                        final ProjectInfo depInfo = env.modelProjects.get(key);
                        if (depInfo != null) {
                            env.logger.debug("Found reactor " + a.getType() + " dependency : " + a);
                            final Model model = addDependencies(env, depInfo);
                            if (model == null) {
                                throw new MavenExecutionException("Recursive model dependency list including project " + info.project, (File) null);
                            }
                            dependencies.add(model);
                            info.includedModels.put(a, depInfo.localModel);
                        } else {
                            env.logger.debug("Found external " + a.getType() + " dependency: " + a);
                            // "external" dependency, we can already resolve it
                            final File modelFile = resolveSlingstartArtifact(env, info.project, dep);
                            FileReader r = null;
                            try {
                                r = new FileReader(modelFile);
                                final Model model = ModelReader.read(r, modelFile.getAbsolutePath());
                                info.includedModels.put(a, model);
                                final Map<Traceable, String> errors = ModelUtility.validate(model);
                                if (errors != null) {
                                    throw new MavenExecutionException("Unable to read model file from " + modelFile + " : " + errors, modelFile);
                                }
                                final Model fullModel = processSlingstartDependencies(env, info, dep, model);
                                dependencies.add(fullModel);
                            } catch (final IOException ioe) {
                                throw new MavenExecutionException("Unable to read model file from " + modelFile, ioe);
                            } finally {
                                try {
                                    if (r != null) {
                                        r.close();
                                    }
                                } catch (final IOException io) {
                                // ignore
                                }
                            }
                        }
                        removeList.add(a);
                    }
                }
                for (final org.apache.sling.provisioning.model.Artifact r : removeList) {
                    group.remove(r);
                    final Feature localModelFeature = rawModel.getFeature(feature.getName());
                    if (localModelFeature != null) {
                        final RunMode localRunMode = localModelFeature.getRunMode(runMode.getNames());
                        if (localRunMode != null) {
                            final ArtifactGroup localAG = localRunMode.getArtifactGroup(group.getStartLevel());
                            if (localAG != null) {
                                localAG.remove(r);
                            }
                        }
                    }
                }
            }
        }
    }
    return dependencies;
}
Also used : ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) MavenExecutionException(org.apache.maven.MavenExecutionException) RunMode(org.apache.sling.provisioning.model.RunMode) Model(org.apache.sling.provisioning.model.Model) FileReader(java.io.FileReader) Traceable(org.apache.sling.provisioning.model.Traceable) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) File(java.io.File)

Example 73 with Dependency

use of org.apache.maven.model.Dependency in project sling by apache.

the class ModelPreprocessor method addDependenciesFromModel.

/**
     * Add all dependencies from the model
     * @param env The environment
     * @param info The project info
     * @param scope The scope which the new dependencies should have
     * @throws MavenExecutionException
     */
private void addDependenciesFromModel(final Environment env, final ProjectInfo info, final String scope) throws MavenExecutionException {
    if (info.project.getPackaging().equals(BuildConstants.PACKAGING_SLINGSTART)) {
        // add base artifact if defined in current model
        final org.apache.sling.provisioning.model.Artifact baseArtifact = ModelUtils.findBaseArtifact(info.model);
        final String[] classifiers = new String[] { null, BuildConstants.CLASSIFIER_APP, BuildConstants.CLASSIFIER_WEBAPP };
        for (final String c : classifiers) {
            final Dependency dep = new Dependency();
            dep.setGroupId(baseArtifact.getGroupId());
            dep.setArtifactId(baseArtifact.getArtifactId());
            dep.setVersion(baseArtifact.getVersion());
            dep.setType(baseArtifact.getType());
            dep.setClassifier(c);
            if (BuildConstants.CLASSIFIER_WEBAPP.equals(c)) {
                dep.setType(BuildConstants.TYPE_WAR);
            }
            dep.setScope(scope);
            info.project.getDependencies().add(dep);
            env.logger.debug("- adding base dependency " + ModelUtils.toString(dep));
        }
    }
    for (final Feature feature : info.model.getFeatures()) {
        // skip launchpad feature
        if (feature.getName().equals(ModelConstants.FEATURE_LAUNCHPAD)) {
            continue;
        }
        for (final RunMode runMode : feature.getRunModes()) {
            for (final ArtifactGroup group : runMode.getArtifactGroups()) {
                for (final org.apache.sling.provisioning.model.Artifact a : group) {
                    if (a.getGroupId().equals(info.project.getGroupId()) && a.getArtifactId().equals(info.project.getArtifactId()) && a.getVersion().equals(info.project.getVersion())) {
                        // skip artifact from the same project
                        env.logger.debug("- skipping dependency " + a.toMvnUrl());
                        continue;
                    }
                    final Dependency dep = new Dependency();
                    dep.setGroupId(a.getGroupId());
                    dep.setArtifactId(a.getArtifactId());
                    dep.setVersion(a.getVersion());
                    dep.setType(a.getType());
                    dep.setClassifier(a.getClassifier());
                    dep.setScope(scope);
                    env.logger.debug("- adding dependency " + ModelUtils.toString(dep));
                    info.project.getDependencies().add(dep);
                }
            }
        }
    }
}
Also used : RunMode(org.apache.sling.provisioning.model.RunMode) Dependency(org.apache.maven.model.Dependency) Feature(org.apache.sling.provisioning.model.Feature) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup)

Aggregations

Dependency (org.apache.maven.model.Dependency)73 ArrayList (java.util.ArrayList)24 Artifact (org.apache.maven.artifact.Artifact)17 File (java.io.File)11 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 Exclusion (org.apache.maven.model.Exclusion)7 WebappStructure (org.apache.maven.plugins.war.util.WebappStructure)7 MavenProject (org.apache.maven.project.MavenProject)7 IOException (java.io.IOException)6 MojoFailureException (org.apache.maven.plugin.MojoFailureException)6 HashMap (java.util.HashMap)5 Model (org.apache.maven.model.Model)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 DependencyManagement (org.apache.maven.model.DependencyManagement)4 Plugin (org.apache.maven.model.Plugin)4 List (java.util.List)3 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)3 Test (org.junit.Test)3 MavenPublishable (com.facebook.buck.jvm.java.MavenPublishable)2