Search in sources :

Example 66 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class InstallFileMojoTest method createMavenSession.

private MavenSession createMavenSession() {
    MavenSession session = mock(MavenSession.class);
    DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManager(new File(LOCAL_REPO)));
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    buildingRequest.setRepositorySession(repositorySession);
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    return session;
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) EnhancedLocalRepositoryManager(org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 67 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class AbstractHelpMojo method getMavenProject.

/**
 * Retrieves the Maven Project associated with the given artifact String, in the form of
 * <code>groupId:artifactId[:version]</code>. This resolves the POM artifact at those coordinates and then builds
 * the Maven project from it.
 *
 * @param artifactString Coordinates of the Maven project to get.
 * @return New Maven project.
 * @throws MojoExecutionException If there was an error while getting the Maven project.
 */
protected MavenProject getMavenProject(String artifactString) throws MojoExecutionException {
    ArtifactCoordinate coordinate = getArtifactCoordinate(artifactString, "pom");
    try {
        ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        pbr.setRemoteRepositories(remoteRepositories);
        pbr.setProject(null);
        pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
        pbr.setResolveDependencies(true);
        Artifact artifact = artifactResolver.resolveArtifact(pbr, coordinate).getArtifact();
        return projectBuilder.build(artifact.getFile(), pbr).getProject();
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to get the POM for the artifact '" + artifactString + "'. Verify the artifact parameter.", e);
    }
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) DefaultArtifactCoordinate(org.apache.maven.shared.artifact.DefaultArtifactCoordinate) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) Artifact(org.apache.maven.artifact.Artifact) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 68 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class AbstractJavadocMojo method getPathElements.

/**
 * Method that gets the classpath and modulepath elements that will be specified in the javadoc
 * <code>-classpath</code> and <code>--module-path</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 all classpath elements
 * @throws MavenReportException if any.
 */
private List<File> getPathElements() throws MavenReportException {
    List<File> classpathElements = new ArrayList<>();
    Map<String, Artifact> compileArtifactMap = new HashMap<>();
    if (isTest()) {
        classpathElements.addAll(getProjectBuildOutputDirs(project));
    }
    populateCompileArtifactMap(compileArtifactMap, project.getArtifacts());
    if (isAggregator() && project.isExecutionRoot()) {
        List<String> reactorArtifacts = new ArrayList<>();
        for (MavenProject p : reactorProjects) {
            reactorArtifacts.add(p.getGroupId() + ':' + p.getArtifactId());
        }
        TransformableFilter dependencyFilter = new AndFilter(Arrays.asList(new PatternExclusionsFilter(reactorArtifacts), getDependencyScopeFilter()));
        for (MavenProject subProject : reactorProjects) {
            if (subProject != project) {
                classpathElements.addAll(getProjectBuildOutputDirs(subProject));
                try {
                    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');
                    ProjectBuildingRequest buildingRequest = session.getProjectBuildingRequest();
                    buildingRequest = buildingRequest.setRemoteRepositories(subProject.getRemoteArtifactRepositories());
                    for (ArtifactResult artifactResult : dependencyResolver.resolveDependencies(buildingRequest, subProject.getDependencies(), null, dependencyFilter)) {
                        populateCompileArtifactMap(compileArtifactMap, Collections.singletonList(artifactResult.getArtifact()));
                        sb.append(artifactResult.getArtifact().getFile()).append('\n');
                    }
                    if (getLog().isDebugEnabled()) {
                        getLog().debug(sb.toString());
                    }
                } catch (DependencyResolverException e) {
                    throw new MavenReportException(e.getMessage(), e);
                }
            }
        }
    }
    for (Artifact a : compileArtifactMap.values()) {
        classpathElements.add(a.getFile());
    }
    if (additionalDependencies != null) {
        for (Dependency dependency : additionalDependencies) {
            Artifact artifact = resolveDependency(dependency);
            getLog().debug("add additional artifact with path " + artifact.getFile());
            classpathElements.add(artifact.getFile());
        }
    }
    return classpathElements;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) PatternExclusionsFilter(org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter) Dependency(org.apache.maven.model.Dependency) BootclasspathArtifact(org.apache.maven.plugins.javadoc.options.BootclasspathArtifact) JavadocPathArtifact(org.apache.maven.plugins.javadoc.options.JavadocPathArtifact) ResourcesArtifact(org.apache.maven.plugins.javadoc.options.ResourcesArtifact) Artifact(org.apache.maven.artifact.Artifact) TagletArtifact(org.apache.maven.plugins.javadoc.options.TagletArtifact) DocletArtifact(org.apache.maven.plugins.javadoc.options.DocletArtifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) AndFilter(org.apache.maven.shared.artifact.filter.resolve.AndFilter) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) TransformableFilter(org.apache.maven.shared.artifact.filter.resolve.TransformableFilter) MavenProject(org.apache.maven.project.MavenProject) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 69 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class JavadocReportTest method testTaglets.

/**
 * Method to test the taglet artifact configuration
 *
 * @throws Exception if any
 */
public void testTaglets() throws Exception {
    // ----------------------------------------------------------------------
    // taglet-test: check if a taglet is used
    // ----------------------------------------------------------------------
    File testPom = new File(unit, "taglet-test/taglet-test-plugin-config.xml");
    JavadocReport mojo = lookupMojo(testPom);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    File apidocs = new File(getBasedir(), "target/test/unit/taglet-test/target/site/apidocs");
    assertTrue(new File(apidocs, "index.html").exists());
    File appFile = new File(apidocs, "taglet/test/App.html");
    assertTrue(appFile.exists());
    String appString = readFile(appFile);
    assertTrue(appString.contains("<b>To Do:</b>"));
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) LegacySupport(org.apache.maven.plugin.LegacySupport) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 70 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class JavadocReportTest method testStylesheetfile.

/**
 * Method to test the <code>&lt;stylesheetfile/&gt;</code> parameter.
 *
 * @throws Exception if any
 */
public void testStylesheetfile() throws Exception {
    File testPom = new File(unit, "stylesheetfile-test/pom.xml");
    JavadocReport mojo = lookupMojo(testPom);
    assertNotNull(mojo);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "session", session);
    File apidocs = new File(getBasedir(), "target/test/unit/stylesheetfile-test/target/site/apidocs");
    File stylesheetfile = new File(apidocs, "stylesheet.css");
    File options = new File(apidocs, "options");
    // stylesheet == maven OR java
    setVariableValueToObject(mojo, "stylesheet", "javamaven");
    try {
        mojo.execute();
        assertTrue(false);
    } catch (Exception e) {
        assertTrue(true);
    }
    // stylesheet == java
    setVariableValueToObject(mojo, "stylesheet", "java");
    mojo.execute();
    String content = readFile(stylesheetfile);
    assertTrue(content.contains("/* Javadoc style sheet */"));
    String optionsContent = readFile(options);
    assertFalse(optionsContent.contains("-stylesheetfile"));
    // stylesheet == maven
    setVariableValueToObject(mojo, "stylesheet", "maven");
    mojo.execute();
    content = readFile(stylesheetfile);
    assertTrue(content.contains("/* Javadoc style sheet */") && content.contains("Licensed to the Apache Software Foundation (ASF) under one"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-stylesheetfile"));
    assertTrue(optionsContent.contains("'" + stylesheetfile.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
    // stylesheetfile defined as a project resource
    setVariableValueToObject(mojo, "stylesheet", null);
    setVariableValueToObject(mojo, "stylesheetfile", "com/mycompany/app/javadoc/css/stylesheet.css");
    mojo.execute();
    content = readFile(stylesheetfile);
    assertTrue(content.contains("/* Custom Javadoc style sheet in project */"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-stylesheetfile"));
    File stylesheetResource = new File(unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css/stylesheet.css");
    assertTrue(optionsContent.contains("'" + stylesheetResource.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
    // stylesheetfile defined in a javadoc plugin dependency
    setVariableValueToObject(mojo, "stylesheetfile", "com/mycompany/app/javadoc/css2/stylesheet.css");
    mojo.execute();
    content = readFile(stylesheetfile);
    assertTrue(content.contains("/* Custom Javadoc style sheet in artefact */"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-stylesheetfile"));
    assertTrue(optionsContent.contains("'" + stylesheetfile.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
    // stylesheetfile defined as file
    File css = new File(unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css");
    setVariableValueToObject(mojo, "stylesheetfile", css.getAbsolutePath());
    mojo.execute();
    content = readFile(stylesheetfile);
    assertTrue(content.contains("/* Custom Javadoc style sheet as file */"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-stylesheetfile"));
    stylesheetResource = new File(unit, "stylesheetfile-test/src/main/resources/com/mycompany/app/javadoc/css3/stylesheet.css");
    assertTrue(optionsContent.contains("'" + stylesheetResource.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) LegacySupport(org.apache.maven.plugin.LegacySupport) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)81 File (java.io.File)40 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)37 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)28 SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)28 Artifact (org.apache.maven.artifact.Artifact)25 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)22 MavenProject (org.apache.maven.project.MavenProject)22 MavenSession (org.apache.maven.execution.MavenSession)17 IOException (java.io.IOException)14 LegacySupport (org.apache.maven.plugin.LegacySupport)13 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)12 ArrayList (java.util.ArrayList)9 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)8 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)8 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)7 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)7 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)7 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)7 ArtifactResult (org.apache.maven.shared.artifact.resolve.ArtifactResult)7