Search in sources :

Example 56 with Dependency

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

the class AntBuildWriter method getUninterpolatedSystemPath.

private String getUninterpolatedSystemPath(Artifact artifact) {
    String managementKey = artifact.getDependencyConflictId();
    for (Dependency dependency : project.getOriginalModel().getDependencies()) {
        if (managementKey.equals(dependency.getManagementKey())) {
            return dependency.getSystemPath();
        }
    }
    for (Profile profile : project.getOriginalModel().getProfiles()) {
        for (Dependency dependency : profile.getDependencies()) {
            if (managementKey.equals(dependency.getManagementKey())) {
                return dependency.getSystemPath();
            }
        }
    }
    String path = artifact.getFile().getAbsolutePath();
    Properties props = new Properties();
    props.putAll(project.getProperties());
    props.putAll(executionProperties);
    props.remove("user.dir");
    props.put("basedir", project.getBasedir().getAbsolutePath());
    SortedMap<String, String> candidateProperties = new TreeMap<String, String>();
    for (Object o : props.keySet()) {
        String key = (String) o;
        String value = new File(props.getProperty(key)).getPath();
        if (path.startsWith(value) && value.length() > 0) {
            candidateProperties.put(value, key);
        }
    }
    if (!candidateProperties.isEmpty()) {
        String value = candidateProperties.lastKey();
        String key = candidateProperties.get(value);
        path = path.substring(value.length());
        path = path.replace('\\', '/');
        return "${" + key + "}" + path;
    }
    return path;
}
Also used : Dependency(org.apache.maven.model.Dependency) Properties(java.util.Properties) TreeMap(java.util.TreeMap) File(java.io.File) Profile(org.apache.maven.model.Profile)

Example 57 with Dependency

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

the class AbstractJavadocMojo method getResource.

/**
     * @param outputFile        not nul
     * @param inputResourceName a not null resource in <code>src/main/java</code>, <code>src/main/resources</code> or
     *                          <code>src/main/javadoc</code> or in the Javadoc plugin dependencies.
     * @return the resource file absolute path as String
     * @since 2.6
     */
private String getResource(File outputFile, String inputResourceName) {
    if (inputResourceName.startsWith("/")) {
        inputResourceName = inputResourceName.replaceFirst("//*", "");
    }
    List<String> classPath = new ArrayList<String>();
    classPath.add(project.getBuild().getSourceDirectory());
    URL resourceURL = getResource(classPath, inputResourceName);
    if (resourceURL != null) {
        getLog().debug(inputResourceName + " found in the main src directory of the project.");
        return FileUtils.toFile(resourceURL).getAbsolutePath();
    }
    classPath.clear();
    List<Resource> resources = project.getBuild().getResources();
    for (Resource resource : resources) {
        classPath.add(resource.getDirectory());
    }
    resourceURL = getResource(classPath, inputResourceName);
    if (resourceURL != null) {
        getLog().debug(inputResourceName + " found in the main resources directories of the project.");
        return FileUtils.toFile(resourceURL).getAbsolutePath();
    }
    if (javadocDirectory.exists()) {
        classPath.clear();
        classPath.add(javadocDirectory.getAbsolutePath());
        resourceURL = getResource(classPath, inputResourceName);
        if (resourceURL != null) {
            getLog().debug(inputResourceName + " found in the main javadoc directory of the project.");
            return FileUtils.toFile(resourceURL).getAbsolutePath();
        }
    }
    classPath.clear();
    final String pluginId = "org.apache.maven.plugins:maven-javadoc-plugin";
    Plugin javadocPlugin = getPlugin(project, pluginId);
    if (javadocPlugin != null && javadocPlugin.getDependencies() != null) {
        List<Dependency> dependencies = javadocPlugin.getDependencies();
        for (Dependency dependency : dependencies) {
            JavadocPathArtifact javadocPathArtifact = new JavadocPathArtifact();
            javadocPathArtifact.setGroupId(dependency.getGroupId());
            javadocPathArtifact.setArtifactId(dependency.getArtifactId());
            javadocPathArtifact.setVersion(dependency.getVersion());
            Artifact artifact = null;
            try {
                artifact = createAndResolveArtifact(javadocPathArtifact);
            } catch (Exception e) {
                logError("Unable to retrieve the dependency: " + dependency + ". Ignored.", e);
            }
            if (artifact != null && artifact.getFile().exists()) {
                classPath.add(artifact.getFile().getAbsolutePath());
            }
        }
        resourceURL = getResource(classPath, inputResourceName);
        if (resourceURL != null) {
            getLog().debug(inputResourceName + " found in javadoc plugin dependencies.");
            try {
                JavadocUtil.copyResource(resourceURL, outputFile);
                return outputFile.getAbsolutePath();
            } catch (IOException e) {
                logError("IOException: " + e.getMessage(), e);
            }
        }
    }
    getLog().warn("Unable to find the resource '" + inputResourceName + "'. Using default Javadoc resources.");
    return null;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) Dependency(org.apache.maven.model.Dependency) IOException(java.io.IOException) URL(java.net.URL) JavadocPathArtifact(org.apache.maven.plugin.javadoc.options.JavadocPathArtifact) Artifact(org.apache.maven.artifact.Artifact) DocletArtifact(org.apache.maven.plugin.javadoc.options.DocletArtifact) BootclasspathArtifact(org.apache.maven.plugin.javadoc.options.BootclasspathArtifact) ResourcesArtifact(org.apache.maven.plugin.javadoc.options.ResourcesArtifact) TagletArtifact(org.apache.maven.plugin.javadoc.options.TagletArtifact) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) InvalidDependencyVersionException(org.apache.maven.project.artifact.InvalidDependencyVersionException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) FileNotFoundException(java.io.FileNotFoundException) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) MalformedURLException(java.net.MalformedURLException) MavenReportException(org.apache.maven.reporting.MavenReportException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) JavadocPathArtifact(org.apache.maven.plugin.javadoc.options.JavadocPathArtifact) Plugin(org.apache.maven.model.Plugin)

Example 58 with Dependency

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

the class AbstractJavadocMojo method getClasspath.

/**
     * Method that sets the classpath elements that will be specified in the javadoc <code>-classpath</code>
     * parameter. Since we have all the sources of the current reactor, it is sufficient to consider the
     * dependencies of the reactor modules, excluding the module artifacts which may not yet be available
     * when the reactor project is built for the first time.
     *
     * @return a String that contains the concatenated classpath elements, separated by the System pathSeparator
     *         string (colon (<code>:</code>) on Solaris or semi-colon (<code>;</code>) on Windows).
     * @throws MavenReportException if any.
     * @see File#pathSeparator
     */
private String getClasspath() throws MavenReportException {
    List<String> classpathElements = new ArrayList<String>();
    Map<String, Artifact> compileArtifactMap = new HashMap<String, Artifact>();
    if (isTest()) {
        classpathElements.addAll(getProjectBuildOutputDirs(project));
    }
    populateCompileArtifactMap(compileArtifactMap, getProjectArtifacts(project));
    if (isAggregator() && project.isExecutionRoot()) {
        List<Artifact> reactorArtifacts = new ArrayList<Artifact>();
        for (MavenProject p : reactorProjects) {
            reactorArtifacts.add(p.getArtifact());
        }
        try {
            for (MavenProject subProject : reactorProjects) {
                if (subProject != project) {
                    classpathElements.addAll(getProjectBuildOutputDirs(subProject));
                    Set<Artifact> dependencyArtifacts = subProject.createArtifacts(factory, null, null);
                    // do not attempt to resolve artifacts of the current reactor which may not exist yet
                    dependencyArtifacts.removeAll(reactorArtifacts);
                    if (!dependencyArtifacts.isEmpty()) {
                        ArtifactResolutionResult result = null;
                        try {
                            result = resolver.resolveTransitively(dependencyArtifacts, subProject.getArtifact(), subProject.getManagedVersionMap(), localRepository, subProject.getRemoteArtifactRepositories(), artifactMetadataSource);
                        } catch (ArtifactNotFoundException e) {
                            throw new MavenReportException(e.getMessage(), e);
                        } catch (ArtifactResolutionException e) {
                            throw new MavenReportException(e.getMessage(), e);
                        }
                        if (result == null) {
                            continue;
                        }
                        populateCompileArtifactMap(compileArtifactMap, getCompileArtifacts(result.getArtifacts()));
                        if (getLog().isDebugEnabled()) {
                            StringBuilder sb = new StringBuilder();
                            sb.append("Compiled artifacts for ");
                            sb.append(subProject.getGroupId()).append(":");
                            sb.append(subProject.getArtifactId()).append(":");
                            sb.append(subProject.getVersion()).append('\n');
                            for (Artifact a : compileArtifactMap.values()) {
                                sb.append(a.getFile()).append('\n');
                            }
                            getLog().debug(sb.toString());
                        }
                    }
                }
            }
        } catch (InvalidDependencyVersionException e) {
            throw new MavenReportException(e.getMessage(), e);
        }
    }
    for (Artifact a : compileArtifactMap.values()) {
        classpathElements.add(a.getFile().toString());
    }
    if (additionalDependencies != null) {
        for (Dependency dependency : additionalDependencies) {
            Artifact artifact = resolveDependency(dependency);
            String path = artifact.getFile().toString();
            getLog().debug("add additional artifact with path " + path);
            classpathElements.add(path);
        }
    }
    return StringUtils.join(classpathElements.iterator(), File.pathSeparator);
}
Also used : HashMap(java.util.HashMap) InvalidDependencyVersionException(org.apache.maven.project.artifact.InvalidDependencyVersionException) ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) JavadocPathArtifact(org.apache.maven.plugin.javadoc.options.JavadocPathArtifact) Artifact(org.apache.maven.artifact.Artifact) DocletArtifact(org.apache.maven.plugin.javadoc.options.DocletArtifact) BootclasspathArtifact(org.apache.maven.plugin.javadoc.options.BootclasspathArtifact) ResourcesArtifact(org.apache.maven.plugin.javadoc.options.ResourcesArtifact) TagletArtifact(org.apache.maven.plugin.javadoc.options.TagletArtifact) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MavenProject(org.apache.maven.project.MavenProject) ArtifactResolutionResult(org.apache.maven.artifact.resolver.ArtifactResolutionResult) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 59 with Dependency

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

the class SubProject1Stub method getDependencies.

@Override
public List<Dependency> getDependencies() {
    Dependency d = new Dependency();
    d.setGroupId("junit");
    d.setArtifactId("junit");
    d.setVersion("3.8.1");
    d.setScope(Artifact.SCOPE_COMPILE);
    return Collections.singletonList(d);
}
Also used : Dependency(org.apache.maven.model.Dependency)

Example 60 with Dependency

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

the class AnalyzeDepMgt method checkDependencyManagement.

/**
     * Does the work of checking the DependencyManagement Section.
     *
     * @return true if errors are found.
     * @throws MojoExecutionException
     */
private boolean checkDependencyManagement() throws MojoExecutionException {
    boolean foundError = false;
    getLog().info("Found Resolved Dependency/DependencyManagement mismatches:");
    List<Dependency> depMgtDependencies = null;
    DependencyManagement depMgt = project.getDependencyManagement();
    if (depMgt != null) {
        depMgtDependencies = depMgt.getDependencies();
    }
    if (depMgtDependencies != null && !depMgtDependencies.isEmpty()) {
        // put all the dependencies from depMgt into a map for quick lookup
        Map<String, Dependency> depMgtMap = new HashMap<String, Dependency>();
        Map<String, Exclusion> exclusions = new HashMap<String, Exclusion>();
        for (Dependency depMgtDependency : depMgtDependencies) {
            depMgtMap.put(depMgtDependency.getManagementKey(), depMgtDependency);
            // now put all the exclusions into a map for quick lookup
            exclusions.putAll(addExclusions(depMgtDependency.getExclusions()));
        }
        // get dependencies for the project (including transitive)
        Set<Artifact> allDependencyArtifacts = new LinkedHashSet<Artifact>(project.getArtifacts());
        // depMgt. That's ok.
        if (this.ignoreDirect) {
            getLog().info("\tIgnoring Direct Dependencies.");
            Set<Artifact> directDependencies = project.getDependencyArtifacts();
            allDependencyArtifacts.removeAll(directDependencies);
        }
        // log exclusion errors
        List<Artifact> exclusionErrors = getExclusionErrors(exclusions, allDependencyArtifacts);
        for (Artifact exclusion : exclusionErrors) {
            getLog().info(StringUtils.stripEnd(getArtifactManagementKey(exclusion), ":") + " was excluded in DepMgt, but version " + exclusion.getVersion() + " has been found in the dependency tree.");
            foundError = true;
        }
        // find and log version mismatches
        Map<Artifact, Dependency> mismatch = getMismatch(depMgtMap, allDependencyArtifacts);
        for (Map.Entry<Artifact, Dependency> entry : mismatch.entrySet()) {
            logMismatch(entry.getKey(), entry.getValue());
            foundError = true;
        }
        if (!foundError) {
            getLog().info("\tNone");
        }
    } else {
        getLog().info("\tNothing in DepMgt.");
    }
    return foundError;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Dependency(org.apache.maven.model.Dependency) Artifact(org.apache.maven.artifact.Artifact) Exclusion(org.apache.maven.model.Exclusion) HashMap(java.util.HashMap) Map(java.util.Map) DependencyManagement(org.apache.maven.model.DependencyManagement)

Aggregations

Dependency (org.apache.maven.model.Dependency)78 ArrayList (java.util.ArrayList)24 Artifact (org.apache.maven.artifact.Artifact)18 File (java.io.File)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)11 MavenProject (org.apache.maven.project.MavenProject)8 IOException (java.io.IOException)7 Exclusion (org.apache.maven.model.Exclusion)7 MojoFailureException (org.apache.maven.plugin.MojoFailureException)7 WebappStructure (org.apache.maven.plugins.war.util.WebappStructure)7 HashMap (java.util.HashMap)6 DependencyManagement (org.apache.maven.model.DependencyManagement)5 Model (org.apache.maven.model.Model)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Plugin (org.apache.maven.model.Plugin)4 List (java.util.List)3 Properties (java.util.Properties)3 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)3