Search in sources :

Example 31 with ProjectBuildingRequest

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

the class AbstractFromConfigurationMojo method getArtifact.

/**
 * Resolves the Artifact from the remote repository if necessary. If no version is specified, it will be retrieved
 * from the dependency list or from the DependencyManagement section of the pom.
 *
 * @param artifactItem containing information about artifact from plugin configuration.
 * @return Artifact object representing the specified file.
 * @throws MojoExecutionException with a message if the version can't be found in DependencyManagement.
 */
protected Artifact getArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
    Artifact artifact;
    try {
        // mdep-50 - rolledback for now because it's breaking some functionality.
        /*
             * List listeners = new ArrayList(); Set theSet = new HashSet(); theSet.add( artifact );
             * ArtifactResolutionResult artifactResolutionResult = artifactCollector.collect( theSet, project
             * .getArtifact(), managedVersions, this.local, project.getRemoteArtifactRepositories(),
             * artifactMetadataSource, null, listeners ); Iterator iter =
             * artifactResolutionResult.getArtifactResolutionNodes().iterator(); while ( iter.hasNext() ) {
             * ResolutionNode node = (ResolutionNode) iter.next(); artifact = node.getArtifact(); }
             */
        ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
        if (localRepositoryDirectory != null) {
            buildingRequest = repositoryManager.setLocalRepositoryBasedir(buildingRequest, localRepositoryDirectory);
        }
        // Map dependency to artifact coordinate
        DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
        coordinate.setGroupId(artifactItem.getGroupId());
        coordinate.setArtifactId(artifactItem.getArtifactId());
        coordinate.setVersion(artifactItem.getVersion());
        coordinate.setClassifier(artifactItem.getClassifier());
        final String extension;
        ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler(artifactItem.getType());
        if (artifactHandler != null) {
            extension = artifactHandler.getExtension();
        } else {
            extension = artifactItem.getType();
        }
        coordinate.setExtension(extension);
        artifact = artifactResolver.resolveArtifact(buildingRequest, coordinate).getArtifact();
    } catch (ArtifactResolverException e) {
        throw new MojoExecutionException("Unable to find/resolve artifact.", e);
    }
    return artifact;
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultArtifactCoordinate(org.apache.maven.shared.artifact.DefaultArtifactCoordinate) Artifact(org.apache.maven.artifact.Artifact)

Example 32 with ProjectBuildingRequest

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

the class RepoBuilderConfigSourceWrapper method getProjectBuildingRequest.

@Override
public ProjectBuildingRequest getProjectBuildingRequest() {
    ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(configSource.getMavenSession().getProjectBuildingRequest());
    pbr.setRemoteRepositories(getProject().getRemoteArtifactRepositories());
    return pbr;
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 33 with ProjectBuildingRequest

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

the class DeployFileMojoTest method testDeployIfArtifactIsNotJar.

public void testDeployIfArtifactIsNotJar() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-artifact-not-jar/plugin-config.xml");
    mojo = (DeployFileMojo) lookupMojo("deploy-file", 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);
    String groupId = (String) getVariableValueFromObject(mojo, "groupId");
    String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
    String version = (String) getVariableValueFromObject(mojo, "version");
    assertEquals("org.apache.maven.test", groupId);
    assertEquals("maven-deploy-file-test", artifactId);
    assertEquals("1.0", version);
    mojo.execute();
    File file = new File(remoteRepo, "deploy-file-artifact-not-jar/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".zip");
    assertTrue(file.exists());
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 34 with ProjectBuildingRequest

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

the class DeployFileMojoTest method testBasicDeployFile.

public void testBasicDeployFile() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml");
    mojo = (DeployFileMojo) lookupMojo("deploy-file", 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);
    String groupId = (String) getVariableValueFromObject(mojo, "groupId");
    String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
    String version = (String) getVariableValueFromObject(mojo, "version");
    String packaging = (String) getVariableValueFromObject(mojo, "packaging");
    File file = (File) getVariableValueFromObject(mojo, "file");
    String repositoryId = (String) getVariableValueFromObject(mojo, "repositoryId");
    String url = (String) getVariableValueFromObject(mojo, "url");
    assertEquals("org.apache.maven.test", groupId);
    assertEquals("maven-deploy-file-test", artifactId);
    assertEquals("1.0", version);
    assertEquals("jar", packaging);
    assertTrue(file.exists());
    assertEquals("deploy-test", repositoryId);
    assertEquals("file://" + getBasedir() + "/target/remote-repo/deploy-file-test", url);
    mojo.execute();
    // check the generated pom
    File pom = new File(remoteRepo, "deploy-file-test/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".pom");
    assertTrue(pom.exists());
    Model model = mojo.readModel(pom);
    assertEquals("4.0.0", model.getModelVersion());
    assertEquals(groupId, model.getGroupId());
    assertEquals(artifactId, model.getArtifactId());
    assertEquals(version, model.getVersion());
    assertEquals(packaging, model.getPackaging());
    assertEquals("POM was created from deploy:deploy-file", model.getDescription());
    // check the remote-repo
    expectedFiles = new ArrayList<String>();
    fileList = new ArrayList<String>();
    File repo = new File(remoteRepo, "deploy-file-test");
    File[] files = repo.listFiles();
    for (File file1 : files) {
        addFileToList(file1, fileList);
    }
    expectedFiles.add("org");
    expectedFiles.add("apache");
    expectedFiles.add("maven");
    expectedFiles.add("test");
    expectedFiles.add("maven-deploy-file-test");
    expectedFiles.add("1.0");
    expectedFiles.add("maven-metadata.xml");
    expectedFiles.add("maven-metadata.xml.md5");
    expectedFiles.add("maven-metadata.xml.sha1");
    expectedFiles.add("maven-deploy-file-test-1.0.jar");
    expectedFiles.add("maven-deploy-file-test-1.0.jar.md5");
    expectedFiles.add("maven-deploy-file-test-1.0.jar.sha1");
    expectedFiles.add("maven-deploy-file-test-1.0.pom");
    expectedFiles.add("maven-deploy-file-test-1.0.pom.md5");
    expectedFiles.add("maven-deploy-file-test-1.0.pom.sha1");
    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) Model(org.apache.maven.model.Model) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 35 with ProjectBuildingRequest

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

the class DeployMojoTest method testBasicDeploy.

public void testBasicDeploy() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/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);
    File file = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/target/" + "deploy-test-file-1.0-SNAPSHOT.jar");
    assertTrue(file.exists());
    MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
    setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
    artifact = (DeployArtifactStub) project.getArtifact();
    String packaging = project.getPackaging();
    assertEquals("jar", packaging);
    artifact.setFile(file);
    ArtifactRepositoryStub repo = getRepoStub(mojo);
    assertNotNull(repo);
    repo.setAppendToUrl("basic-deploy-test");
    assertEquals("deploy-test", repo.getId());
    assertEquals("deploy-test", repo.getKey());
    assertEquals("file", repo.getProtocol());
    assertEquals("file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl());
    mojo.execute();
    // check the artifact in local 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-deploy-test.xml");
    // expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
    // expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
    // as we are in SNAPSHOT the file is here twice
    expectedFiles.add("maven-metadata-deploy-test.xml");
    // extra Aether files
    expectedFiles.add("resolver-status.properties");
    expectedFiles.add("resolver-status.properties");
    File localRepo = new File(LOCAL_REPO, "");
    File[] files = localRepo.listFiles();
    for (File file2 : files) {
        addFileToList(file2, fileList);
    }
    assertEquals(expectedFiles.size(), fileList.size());
    assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
    // check the artifact in remote repository
    expectedFiles = new ArrayList<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");
    remoteRepo = new File(remoteRepo, "basic-deploy-test");
    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)

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