Search in sources :

Example 61 with ProjectBuildingRequest

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

the class DeployMojoTest method testUpdateReleaseParamSetToTrue.

public void testUpdateReleaseParamSetToTrue() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-pom/plugin-config.xml");
    mojo = (DeployMojo) lookupMojo("deploy", testPom);
    MockitoAnnotations.initMocks(this);
    assertNotNull(mojo);
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(LOCAL_REPO));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    boolean updateReleaseInfo = (Boolean) getVariableValueFromObject(mojo, "updateReleaseInfo");
    assertTrue(updateReleaseInfo);
    MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
    setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
    artifact = (DeployArtifactStub) project.getArtifact();
    artifact.setFile(testPom);
    ArtifactRepositoryStub repo = getRepoStub(mojo);
    repo.setAppendToUrl("basic-deploy-updateReleaseParam");
    mojo.execute();
    assertTrue(artifact.isRelease());
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenProject(org.apache.maven.project.MavenProject) ArtifactRepositoryStub(org.apache.maven.plugins.deploy.stubs.ArtifactRepositoryStub) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 62 with ProjectBuildingRequest

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

the class DeployMojoTest method testDeployWithAttachedArtifacts.

public void testDeployWithAttachedArtifacts() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-with-attached-artifacts/" + "plugin-config.xml");
    mojo = (DeployMojo) lookupMojo("deploy", testPom);
    MockitoAnnotations.initMocks(this);
    assertNotNull(mojo);
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(LOCAL_REPO));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
    setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
    artifact = (DeployArtifactStub) project.getArtifact();
    File file = new File(getBasedir(), "target/test-classes/unit/basic-deploy-with-attached-artifacts/target/" + "deploy-test-file-1.0-SNAPSHOT.jar");
    artifact.setFile(file);
    ArtifactRepositoryStub repo = getRepoStub(mojo);
    repo.setAppendToUrl("basic-deploy-with-attached-artifacts");
    mojo.execute();
    // check the artifacts in remote repository
    List<String> expectedFiles = new ArrayList<String>();
    List<String> fileList = new ArrayList<String>();
    expectedFiles.add("org");
    expectedFiles.add("apache");
    expectedFiles.add("maven");
    expectedFiles.add("test");
    expectedFiles.add("maven-deploy-test");
    expectedFiles.add("1.0-SNAPSHOT");
    expectedFiles.add("maven-metadata.xml");
    expectedFiles.add("maven-metadata.xml.md5");
    expectedFiles.add("maven-metadata.xml.sha1");
    expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar");
    expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar.md5");
    expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar.sha1");
    expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom");
    expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.md5");
    expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.sha1");
    // as we are in SNAPSHOT the file is here twice
    expectedFiles.add("maven-metadata.xml");
    expectedFiles.add("maven-metadata.xml.md5");
    expectedFiles.add("maven-metadata.xml.sha1");
    expectedFiles.add("attached-artifact-test-0");
    expectedFiles.add("1.0-SNAPSHOT");
    expectedFiles.add("maven-metadata.xml");
    expectedFiles.add("maven-metadata.xml.md5");
    expectedFiles.add("maven-metadata.xml.sha1");
    expectedFiles.add("attached-artifact-test-0-1.0-SNAPSHOT.jar");
    expectedFiles.add("attached-artifact-test-0-1.0-SNAPSHOT.jar.md5");
    expectedFiles.add("attached-artifact-test-0-1.0-SNAPSHOT.jar.sha1");
    // as we are in SNAPSHOT the file is here twice
    expectedFiles.add("maven-metadata.xml");
    expectedFiles.add("maven-metadata.xml.md5");
    expectedFiles.add("maven-metadata.xml.sha1");
    remoteRepo = new File(remoteRepo, "basic-deploy-with-attached-artifacts");
    File[] files = remoteRepo.listFiles();
    for (File file1 : files) {
        addFileToList(file1, fileList);
    }
    assertEquals(expectedFiles.size(), fileList.size());
    assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenProject(org.apache.maven.project.MavenProject) ArtifactRepositoryStub(org.apache.maven.plugins.deploy.stubs.ArtifactRepositoryStub) ArrayList(java.util.ArrayList) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 63 with ProjectBuildingRequest

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

the class DescribeMojo method isReportGoal.

/**
 * Determines if this Mojo should be used as a report or not. This resolves the plugin project along with all of its
 * transitive dependencies to determine if the Java class of this goal implements <code>MavenReport</code>.
 *
 * @param md Mojo descriptor
 * @return Whether or not this goal should be used as a report.
 */
private boolean isReportGoal(MojoDescriptor md) {
    PluginDescriptor pd = md.getPluginDescriptor();
    List<URL> urls = new ArrayList<URL>();
    ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
    pbr.setRemoteRepositories(remoteRepositories);
    pbr.setResolveDependencies(true);
    pbr.setProject(null);
    pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    try {
        Artifact jar = artifactResolver.resolveArtifact(pbr, toArtifactCoordinate(pd, "jar")).getArtifact();
        Artifact pom = artifactResolver.resolveArtifact(pbr, toArtifactCoordinate(pd, "pom")).getArtifact();
        MavenProject project = projectBuilder.build(pom.getFile(), pbr).getProject();
        urls.add(jar.getFile().toURI().toURL());
        for (Object artifact : project.getCompileClasspathElements()) {
            urls.add(new File((String) artifact).toURI().toURL());
        }
        ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
        return MavenReport.class.isAssignableFrom(Class.forName(md.getImplementation(), false, classLoader));
    } catch (Exception e) {
        getLog().warn("Couldn't identify if this goal is a report goal: " + e.getMessage());
        return false;
    }
}
Also used : ArrayList(java.util.ArrayList) URL(java.net.URL) Artifact(org.apache.maven.artifact.Artifact) PluginVersionResolutionException(org.apache.maven.plugin.version.PluginVersionResolutionException) NoPluginFoundForPrefixException(org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) MavenProject(org.apache.maven.project.MavenProject) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 64 with ProjectBuildingRequest

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

the class InstallMojoTest 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 65 with ProjectBuildingRequest

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

the class InstallMojoTest method testBasicInstallAndCreateChecksumIsTrue.

public void testBasicInstallAndCreateChecksumIsTrue() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml");
    AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo("install", testPom);
    assertNotNull(mojo);
    File file = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar");
    MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
    MavenSession mavenSession = createMavenSession();
    updateMavenProject(project);
    setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
    setVariableValueToObject(mojo, "session", mavenSession);
    artifact = (InstallArtifactStub) project.getArtifact();
    boolean createChecksum = (Boolean) getVariableValueFromObject(mojo, "createChecksum");
    assertTrue(createChecksum);
    artifact.setFile(file);
    mojo.execute();
    ArtifactMetadata metadata = null;
    for (Object o : artifact.getMetadataList()) {
        metadata = (ArtifactMetadata) o;
        if (metadata.getRemoteFilename().endsWith("pom")) {
            break;
        }
    }
    RepositoryManager repoManager = (RepositoryManager) getVariableValueFromObject(mojo, "repositoryManager");
    ProjectBuildingRequest pbr = mavenSession.getProjectBuildingRequest();
    File pom = new File(repoManager.getLocalRepositoryBasedir(pbr), repoManager.getPathForLocalMetadata(pbr, metadata));
    assertTrue(pom.exists());
    String groupId = dotToSlashReplacer(artifact.getGroupId());
    String packaging = project.getPackaging();
    String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion();
    // get the actual checksum of the pom
    Map<String, Object> csums = ChecksumUtils.calc(pom, Utils.CHECKSUM_ALGORITHMS);
    for (Map.Entry<String, Object> csum : csums.entrySet()) {
        Object actualPomSum = csum.getValue();
        File pomSum = new File(localPath + ".pom." + csum.getKey().toLowerCase().replace("-", ""));
        assertTrue(pomSum.exists());
        String generatedPomSum = FileUtils.fileRead(pomSum, "UTF-8");
        assertEquals(actualPomSum, generatedPomSum);
    }
    // get the actual checksum of the artifact
    csums = ChecksumUtils.calc(file, Utils.CHECKSUM_ALGORITHMS);
    for (Map.Entry<String, Object> csum : csums.entrySet()) {
        Object actualSum = csum.getValue();
        File sum = new File(localPath + "." + packaging + "." + csum.getKey().toLowerCase().replace("-", ""));
        assertTrue(sum.exists());
        String generatedSum = FileUtils.fileRead(sum, "UTF-8");
        assertEquals(actualSum, generatedSum);
    }
    File installedArtifact = new File(localPath + "." + packaging);
    assertTrue(installedArtifact.exists());
    assertEquals(9, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) MavenProject(org.apache.maven.project.MavenProject) AbstractInstallMojo(org.apache.maven.plugins.install.AbstractInstallMojo) EnhancedLocalRepositoryManager(org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager) RepositoryManager(org.apache.maven.shared.repository.RepositoryManager) File(java.io.File) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) Map(java.util.Map)

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