Search in sources :

Example 1 with ProjectArtifactMetadata

use of org.apache.maven.project.artifact.ProjectArtifactMetadata in project maven-plugins by apache.

the class InstallFileMojo method execute.

/**
     * @see org.apache.maven.plugin.Mojo#execute()
     */
public void execute() throws MojoExecutionException, MojoFailureException {
    if (!file.exists()) {
        String message = "The specified file '" + file.getPath() + "' not exists";
        getLog().error(message);
        throw new MojoFailureException(message);
    }
    ProjectBuildingRequest buildingRequest = session.getProjectBuildingRequest();
    // ----------------------------------------------------------------------
    if (localRepositoryPath != null) {
        buildingRequest = repositoryManager.setLocalRepositoryBasedir(buildingRequest, localRepositoryPath);
        getLog().debug("localRepoPath: " + repositoryManager.getLocalRepositoryBasedir(buildingRequest));
    }
    File temporaryPom = null;
    if (pomFile != null) {
        processModel(readModel(pomFile));
    } else {
        temporaryPom = readingPomFromJarFile();
        pomFile = temporaryPom;
    }
    MavenProject project = createMavenProject();
    // We need to set a new ArtifactHandler otherwise 
    // the extension will be set to the packaging type
    // which is sometimes wrong.
    DefaultArtifactHandler ah = new DefaultArtifactHandler(packaging);
    ah.setExtension(FileUtils.getExtension(file.getName()));
    project.getArtifact().setArtifactHandler(ah);
    Artifact artifact = project.getArtifact();
    if (file.equals(getLocalRepoFile(buildingRequest, artifact))) {
        throw new MojoFailureException("Cannot install artifact. " + "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n");
    }
    if (classifier == null) {
        artifact.setFile(file);
        if ("pom".equals(packaging)) {
            project.setFile(file);
        }
    } else {
        projectHelper.attachArtifact(project, packaging, classifier, file);
    }
    if (!"pom".equals(packaging)) {
        if (pomFile != null) {
            if (classifier == null) {
                artifact.addMetadata(new ProjectArtifactMetadata(artifact, pomFile));
            } else {
                project.setFile(pomFile);
            }
        } else {
            temporaryPom = generatePomFile();
            ProjectArtifactMetadata pomMetadata = new ProjectArtifactMetadata(artifact, temporaryPom);
            if (Boolean.TRUE.equals(generatePom) || (generatePom == null && !getLocalRepoFile(buildingRequest, pomMetadata).exists())) {
                getLog().debug("Installing generated POM");
                if (classifier == null) {
                    artifact.addMetadata(pomMetadata);
                } else {
                    project.setFile(temporaryPom);
                }
            } else if (generatePom == null) {
                getLog().debug("Skipping installation of generated POM, already present in local repository");
            }
        }
    }
    if (sources != null) {
        projectHelper.attachArtifact(project, "jar", "sources", sources);
    }
    if (javadoc != null) {
        projectHelper.attachArtifact(project, "jar", "javadoc", javadoc);
    }
    try {
        // CHECKSTYLE_OFF: LineLength
        ProjectInstallerRequest projectInstallerRequest = new ProjectInstallerRequest().setProject(project).setCreateChecksum(createChecksum).setUpdateReleaseInfo(updateReleaseInfo);
        // CHECKSTYLE_ON: LineLength
        installer.install(buildingRequest, projectInstallerRequest);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (temporaryPom != null) {
            // noinspection ResultOfMethodCallIgnored
            temporaryPom.delete();
        }
    }
}
Also used : ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) ProjectInstallerRequest(org.apache.maven.shared.project.install.ProjectInstallerRequest) MavenProject(org.apache.maven.project.MavenProject) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) JarFile(java.util.jar.JarFile) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 2 with ProjectArtifactMetadata

use of org.apache.maven.project.artifact.ProjectArtifactMetadata in project maven-plugins by apache.

the class DeployFileMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    if (uniqueVersion != null) {
        throw new MojoExecutionException("You are using 'uniqueVersion' which has been removed" + " from the maven-deploy-plugin. " + "Please see the >>Major Version Upgrade to version 3.0.0<< on the plugin site.");
    }
    failIfOffline();
    if (!file.exists()) {
        throw new MojoExecutionException(file.getPath() + " not found.");
    }
    initProperties();
    ArtifactRepository deploymentRepository = createDeploymentArtifactRepository(repositoryId, url);
    String protocol = deploymentRepository.getProtocol();
    if (StringUtils.isEmpty(protocol)) {
        throw new MojoExecutionException("No transfer protocol found.");
    }
    MavenProject project = createMavenProject();
    Artifact artifact = project.getArtifact();
    if (file.equals(getLocalRepoFile())) {
        throw new MojoFailureException("Cannot deploy artifact from the local repository: " + file);
    }
    List<Artifact> deployableArtifacts = new ArrayList<Artifact>();
    if (classifier == null) {
        artifact.setFile(file);
        deployableArtifacts.add(artifact);
    } else {
        projectHelper.attachArtifact(project, packaging, classifier, file);
    }
    // Upload the POM if requested, generating one if need be
    if (!"pom".equals(packaging)) {
        File pom = pomFile;
        if (pom == null && generatePom) {
            pom = generatePomFile();
        }
        if (pom != null) {
            if (classifier == null) {
                ProjectArtifactMetadata metadata = new ProjectArtifactMetadata(artifact, pom);
                artifact.addMetadata(metadata);
            } else {
                artifact.setFile(pom);
                deployableArtifacts.add(artifact);
            }
        }
    }
    if (updateReleaseInfo) {
        artifact.setRelease(true);
    }
    artifact.setRepository(deploymentRepository);
    if (sources != null) {
        projectHelper.attachArtifact(project, "jar", "sources", sources);
    }
    if (javadoc != null) {
        projectHelper.attachArtifact(project, "jar", "javadoc", javadoc);
    }
    if (files != null) {
        if (types == null) {
            throw new MojoExecutionException("You must specify 'types' if you specify 'files'");
        }
        if (classifiers == null) {
            throw new MojoExecutionException("You must specify 'classifiers' if you specify 'files'");
        }
        int filesLength = StringUtils.countMatches(files, ",");
        int typesLength = StringUtils.countMatches(types, ",");
        int classifiersLength = StringUtils.countMatches(classifiers, ",");
        if (typesLength != filesLength) {
            throw new MojoExecutionException("You must specify the same number of entries in 'files' and " + "'types' (respectively " + filesLength + " and " + typesLength + " entries )");
        }
        if (classifiersLength != filesLength) {
            throw new MojoExecutionException("You must specify the same number of entries in 'files' and " + "'classifiers' (respectively " + filesLength + " and " + classifiersLength + " entries )");
        }
        int fi = 0;
        int ti = 0;
        int ci = 0;
        for (int i = 0; i <= filesLength; i++) {
            int nfi = files.indexOf(',', fi);
            if (nfi == -1) {
                nfi = files.length();
            }
            int nti = types.indexOf(',', ti);
            if (nti == -1) {
                nti = types.length();
            }
            int nci = classifiers.indexOf(',', ci);
            if (nci == -1) {
                nci = classifiers.length();
            }
            File file = new File(files.substring(fi, nfi));
            if (!file.isFile()) {
                // try relative to the project basedir just in case
                file = new File(project.getBasedir(), files.substring(fi, nfi));
            }
            if (file.isFile()) {
                if (StringUtils.isWhitespace(classifiers.substring(ci, nci))) {
                    projectHelper.attachArtifact(project, types.substring(ti, nti).trim(), file);
                } else {
                    projectHelper.attachArtifact(project, types.substring(ti, nti).trim(), classifiers.substring(ci, nci).trim(), file);
                }
            } else {
                throw new MojoExecutionException("Specified side artifact " + file + " does not exist");
            }
            fi = nfi + 1;
            ti = nti + 1;
            ci = nci + 1;
        }
    } else {
        if (types != null) {
            throw new MojoExecutionException("You must specify 'files' if you specify 'types'");
        }
        if (classifiers != null) {
            throw new MojoExecutionException("You must specify 'files' if you specify 'classifiers'");
        }
    }
    List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
    for (Artifact attached : attachedArtifacts) {
        deployableArtifacts.add(attached);
    }
    try {
        artifactDeployer.deploy(getSession().getProjectBuildingRequest(), deploymentRepository, deployableArtifacts);
    } catch (ArtifactDeployerException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenProject(org.apache.maven.project.MavenProject) ArtifactDeployerException(org.apache.maven.shared.artifact.deploy.ArtifactDeployerException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) JarFile(java.util.jar.JarFile) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 3 with ProjectArtifactMetadata

use of org.apache.maven.project.artifact.ProjectArtifactMetadata in project maven-plugins by apache.

the class SignAndDeployFileMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    AbstractGpgSigner signer = newSigner(null);
    signer.setOutputDirectory(ascDirectory);
    signer.setBaseDirectory(new File("").getAbsoluteFile());
    if (offline) {
        throw new MojoFailureException("Cannot deploy artifacts when Maven is in offline mode");
    }
    initProperties();
    validateArtifactInformation();
    if (!file.exists()) {
        throw new MojoFailureException(file.getPath() + " not found.");
    }
    ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get(repositoryLayout);
    if (layout == null) {
        throw new MojoFailureException("Invalid repository layout: " + repositoryLayout);
    }
    ArtifactRepository deploymentRepository = repositoryFactory.createDeploymentArtifactRepository(repositoryId, url, layout, uniqueVersion);
    if (StringUtils.isEmpty(deploymentRepository.getProtocol())) {
        throw new MojoFailureException("No transfer protocol found.");
    }
    Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, packaging, classifier);
    if (file.equals(getLocalRepoFile(artifact))) {
        throw new MojoFailureException("Cannot deploy artifact from the local repository: " + file);
    }
    File fileSig = signer.generateSignatureForArtifact(file);
    ArtifactMetadata metadata = new AscArtifactMetadata(artifact, fileSig, false);
    artifact.addMetadata(metadata);
    if (!"pom".equals(packaging)) {
        if (pomFile == null && generatePom) {
            pomFile = generatePomFile();
        }
        if (pomFile != null) {
            metadata = new ProjectArtifactMetadata(artifact, pomFile);
            artifact.addMetadata(metadata);
            fileSig = signer.generateSignatureForArtifact(pomFile);
            metadata = new AscArtifactMetadata(artifact, fileSig, true);
            artifact.addMetadata(metadata);
        }
    }
    if (updateReleaseInfo) {
        artifact.setRelease(true);
    }
    project.setArtifact(artifact);
    try {
        deploy(file, artifact, deploymentRepository, localRepository);
    } catch (ArtifactDeploymentException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    if (sources != null) {
        projectHelper.attachArtifact(project, "jar", "sources", sources);
    }
    if (javadoc != null) {
        projectHelper.attachArtifact(project, "jar", "javadoc", javadoc);
    }
    if (files != null) {
        if (types == null) {
            throw new MojoExecutionException("You must specify 'types' if you specify 'files'");
        }
        if (classifiers == null) {
            throw new MojoExecutionException("You must specify 'classifiers' if you specify 'files'");
        }
        int filesLength = StringUtils.countMatches(files, ",");
        int typesLength = StringUtils.countMatches(types, ",");
        int classifiersLength = StringUtils.countMatches(classifiers, ",");
        if (typesLength != filesLength) {
            throw new MojoExecutionException("You must specify the same number of entries in 'files' and " + "'types' (respectively " + filesLength + " and " + typesLength + " entries )");
        }
        if (classifiersLength != filesLength) {
            throw new MojoExecutionException("You must specify the same number of entries in 'files' and " + "'classifiers' (respectively " + filesLength + " and " + classifiersLength + " entries )");
        }
        int fi = 0;
        int ti = 0;
        int ci = 0;
        for (int i = 0; i <= filesLength; i++) {
            int nfi = files.indexOf(',', fi);
            if (nfi == -1) {
                nfi = files.length();
            }
            int nti = types.indexOf(',', ti);
            if (nti == -1) {
                nti = types.length();
            }
            int nci = classifiers.indexOf(',', ci);
            if (nci == -1) {
                nci = classifiers.length();
            }
            File file = new File(files.substring(fi, nfi));
            if (!file.isFile()) {
                // try relative to the project basedir just in case
                file = new File(project.getBasedir(), files.substring(fi, nfi));
            }
            if (file.isFile()) {
                if (StringUtils.isWhitespace(classifiers.substring(ci, nci))) {
                    projectHelper.attachArtifact(project, types.substring(ti, nti).trim(), file);
                } else {
                    projectHelper.attachArtifact(project, types.substring(ti, nti).trim(), classifiers.substring(ci, nci).trim(), file);
                }
            } else {
                throw new MojoExecutionException("Specified side artifact " + file + " does not exist");
            }
            fi = nfi + 1;
            ti = nti + 1;
            ci = nci + 1;
        }
    } else {
        if (types != null) {
            throw new MojoExecutionException("You must specify 'files' if you specify 'types'");
        }
        if (classifiers != null) {
            throw new MojoExecutionException("You must specify 'files' if you specify 'classifiers'");
        }
    }
    List attachedArtifacts = project.getAttachedArtifacts();
    for (Object attachedArtifact : attachedArtifacts) {
        Artifact attached = (Artifact) attachedArtifact;
        fileSig = signer.generateSignatureForArtifact(attached.getFile());
        attached = new AttachedSignedArtifact(attached, new AscArtifactMetadata(attached, fileSig, false));
        try {
            deploy(attached.getFile(), attached, deploymentRepository, localRepository);
        } catch (ArtifactDeploymentException e) {
            throw new MojoExecutionException("Error deploying attached artifact " + attached.getFile() + ": " + e.getMessage(), e);
        }
    }
}
Also used : ArtifactRepositoryLayout(org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout) ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactDeploymentException(org.apache.maven.artifact.deployer.ArtifactDeploymentException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) Artifact(org.apache.maven.artifact.Artifact) List(java.util.List) File(java.io.File) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata)

Aggregations

File (java.io.File)3 Artifact (org.apache.maven.artifact.Artifact)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 ProjectArtifactMetadata (org.apache.maven.project.artifact.ProjectArtifactMetadata)3 JarFile (java.util.jar.JarFile)2 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)2 MavenProject (org.apache.maven.project.MavenProject)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ArtifactDeploymentException (org.apache.maven.artifact.deployer.ArtifactDeploymentException)1 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)1 ArtifactMetadata (org.apache.maven.artifact.metadata.ArtifactMetadata)1 ArtifactRepositoryLayout (org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout)1 ModelBuildingException (org.apache.maven.model.building.ModelBuildingException)1 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)1 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)1 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)1