Example usage for org.apache.maven.project MavenProject getVersion

List of usage examples for org.apache.maven.project MavenProject getVersion

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getVersion.

Prototype

public String getVersion() 

Source Link

Usage

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

public List<String> findVersions(Artifact artifact) {

    List<String> versions = new ArrayList<String>();
    String key = ArtifactUtils.versionlessKey(artifact.getGroupId(), artifact.getArtifactId());
    List<MavenProject> projects = buildProjectsByGA.get(key);

    if (projects != null) {
        for (MavenProject project : projects) {
            File artifactFile = find(project, artifact);
            if (artifactFile != null) {
                versions.add(project.getVersion());
            }//from w w  w .  j  a v a 2s .  c om
        }
    }

    if (versions.isEmpty() && workspaceResolutionEnabled) {
        projects = workspaceProjectsByGA.get(key);
        if (projects != null) {
            for (MavenProject project : projects) {
                File artifactFile = findWorkspaceArtifact(project, artifact);
                if (artifactFile != null) {
                    versions.add(project.getVersion());
                }
            }
        }
    }

    return Collections.unmodifiableList(versions);
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

private File find(MavenProject project, Artifact artifact) {
    File file = null;/*ww w .j a  v  a  2  s  . c  om*/

    if ("pom".equals(artifact.getExtension())) {
        return project.getFile();
    }

    //
    // project = project where we will find artifact, this may be the primary artifact or any of the 
    // secondary artifacts
    //
    Artifact projectArtifact = findMatchingArtifact(project, artifact);

    if (hasArtifactFileFromPackagePhase(projectArtifact)) {
        //
        // We have gone far enough in the lifecycle to produce a JAR, WAR, or other file-based artifact
        //
        if (isTestArtifact(artifact)) {
            //
            // We are looking for a test JAR foo-1.0-test.jar
            //
            file = new File(project.getBuild().getDirectory(),
                    String.format("%s-%s-tests.jar", project.getArtifactId(), project.getVersion()));
        } else {
            //
            // We are looking for an application JAR foo-1.0.jar
            //
            file = projectArtifact.getFile();
        }
    } else if (!hasBeenPackaged(project)) {
        //
        // Here no file has been produced so we fallback to loose class files only if artifacts haven't been packaged yet
        // and only for plain old jars. Not war files, not ear files, not anything else.
        //                    
        String type = artifact.getProperty("type", "");

        if (isTestArtifact(artifact)) {
            if (project.hasLifecyclePhase("test-compile")) {
                file = new File(project.getBuild().getTestOutputDirectory());
            }
        } else if (project.hasLifecyclePhase("compile") && COMPILE_PHASE_TYPES.contains(type)) {
            file = new File(project.getBuild().getOutputDirectory());
        }
        if (file == null && allowArtifactsWithoutAFileToBeResolvedInTheReactor) {
            //
            // There is no elegant way to signal that the Artifact's representation is actually present in the 
            // reactor but that it has no file-based representation during this execution and may, in fact, not
            // require one. The case this accounts for something I am doing with Eclipse project file
            // generation where I can perform dependency resolution, but cannot run any lifecycle phases.
            // I need the in-reactor references to work correctly. The WorkspaceReader interface needs
            // to be changed to account for this. This is not exactly elegant, but it's turned on
            // with a property and should not interfere.
            //
            // We have made a slight change in that we don't want to return "." because it confuses the 
            // compiler plugin for special cases that we have where we are running the compiler in the
            // process-resources phase.
            //
            file = new File(project.getBuild().getOutputDirectory());
            //if (file.exists() == false) {
            //  file = new File(".");
            //}
        }
    }

    //
    // The fall-through indicates that the artifact cannot be found;
    // for instance if package produced nothing or classifier problems.
    //
    return file;
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

private File findWorkspaceArtifact(MavenProject project, Artifact artifact) {
    File file = null;//from  www  . j a  v a  2  s  .  c  o m

    if ("pom".equals(artifact.getExtension())) {
        return project.getFile();
    }

    //
    // project = project where we will find artifact, this may be the primary artifact or any of the 
    // secondary artifacts
    //
    Artifact projectArtifact = findMatchingArtifact(project, artifact);

    if (hasArtifactFileFromPackagePhase(projectArtifact)) {
        //
        // We have gone far enough in the lifecycle to produce a JAR, WAR, or other file-based artifact
        //
        if (isTestArtifact(artifact)) {
            //
            // We are looking for a test JAR foo-1.0-test.jar
            //
            file = new File(project.getBuild().getDirectory(),
                    String.format("%s-%s-tests.jar", project.getArtifactId(), project.getVersion()));
        } else {
            //
            // We are looking for an application JAR foo-1.0.jar
            //
            file = projectArtifact.getFile();
        }
    } else if (!hasBeenPackaged(project)) {
        //
        // Here no file has been produced so we fallback to loose class files only if artifacts haven't been packaged yet
        // and only for plain old jars. Not war files, not ear files, not anything else.
        //                    
        String type = artifact.getProperty("type", "");

        if (isTestArtifact(artifact)) {
            if (project.hasLifecyclePhase("test-compile")) {
                file = new File(project.getBuild().getTestOutputDirectory());
            }
        } else if (project.hasLifecyclePhase("compile") && COMPILE_PHASE_TYPES.contains(type)) {
            file = new File(project.getBuild().getOutputDirectory());
        }
        if (file == null && allowArtifactsWithoutAFileToBeResolvedInTheReactor) {
            //
            // There is no elegant way to signal that the Artifact's representation is actually present in the 
            // reactor but that it has no file-based representation during this execution and may, in fact, not
            // require one. The case this accounts for something I am doing with Eclipse project file
            // generation where I can perform dependency resolution, but cannot run any lifecycle phases.
            // I need the in-reactor references to work correctly. The WorkspaceReader interface needs
            // to be changed to account for this. This is not exactly elegant, but it's turned on
            // with a property and should not interfere.
            // 

            /*
                    
            Turn off resolving for now
                    
            file = new File(project.getBuild().getOutputDirectory());
            if (file.exists() == false) {
              file = new File(".");
            }
                    
            */
        }
    }

    //
    // The fall-through indicates that the artifact cannot be found;
    // for instance if package produced nothing or classifier problems.
    //
    return file;
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

private Map<String, MavenProject> getProjectMap(Collection<MavenProject> projects) {
    Map<String, MavenProject> index = new LinkedHashMap<String, MavenProject>();
    Map<String, List<File>> collisions = new LinkedHashMap<String, List<File>>();

    for (MavenProject project : projects) {
        String projectId = ArtifactUtils.key(project.getGroupId(), project.getArtifactId(),
                project.getVersion());

        MavenProject collision = index.get(projectId);

        if (collision == null) {
            index.put(projectId, project);
        } else {/* ww w. j  a va2  s  .  c  om*/
            List<File> pomFiles = collisions.get(projectId);

            if (pomFiles == null) {
                pomFiles = new ArrayList<File>(Arrays.asList(collision.getFile(), project.getFile()));
                collisions.put(projectId, pomFiles);
            } else {
                pomFiles.add(project.getFile());
            }
        }
    }

    /*
     * 
     * We'll assume there is no collisions
     * 
     * if (!collisions.isEmpty()) { throw new DuplicateProjectException("Two or more projects in the reactor" + " have the same identifier, please make sure that <groupId>:<artifactId>:<version>" +
     * " is unique for each project: " + collisions, collisions); }
     */

    return index;
}

From source file:io.teecube.t3.GenericReplacerMojo.java

License:Apache License

@Override
public void execute(ExecutionContext context) throws MojoExecutionException, MojoFailureException {
    this.log.info("Perform replacement in POMs.");

    // prepare for rollback
    this.cachedPOMs = Maps.newHashMap();
    for (MavenProject p : this.reactorProjects) {
        this.cachedPOMs.put(p, PomUtil.parsePOM(p).get());
    }/*from   w w  w .j  a v a 2s.c  o m*/

    // retrieve qualifier (for instance the 1 in genericReplacer[1])
    String _qualifier = context.getQualifier() != null ? context.getQualifier() : "1";
    String _signGPG = "false";
    if (_qualifier.contains(",")) {
        _signGPG = _qualifier.substring(_qualifier.indexOf(",") + 1).trim();
        _qualifier = _qualifier.substring(0, _qualifier.indexOf(",")).trim();
    }
    qualifier = Integer.parseInt(_qualifier); // TODO: handle parse error
    signGPG = new Boolean(_signGPG);
    this.log.info("Step number is " + qualifier);

    try {
        for (MavenProject p : this.reactorProjects) {
            if (qualifier == 2) {
                for (String profile : profiles) {
                    session.getSettings().addActiveProfile(profile);
                }
                p.getModel().getProperties().put("ecosystemVersion", p.getVersion());
            }
            propertiesManager = CommonMojo.propertiesManager(session, p);

            if (doReplace(p.getFile(), RegexpUtil.asOptions(""))) {
                if (signGPG) {
                    signGPG(p);
                }
            }
        }
    } catch (Throwable t) {
        throw new MojoFailureException("Could not perform replacement in POMs.", t);
    } finally {
        deleteTempSettings();
    }
}

From source file:io.teecube.t3.GenericReplacerMojo.java

License:Apache License

private void signGPG(MavenProject p)
        throws MojoExecutionException, MavenInvocationException, MojoFailureException, IOException {
    InvocationRequest request = getInvocationRequest(p);
    Invoker invoker = getInvoker();

    InvocationResult result = invoker.execute(request);
    if (result.getExitCode() != 0) {
        CommandLineException executionException = result.getExecutionException();
        if (executionException != null) {
            throw new MojoFailureException("Error during project build: " + executionException.getMessage(),
                    executionException);
        } else {/*  w  w w . ja v  a  2 s.  co  m*/
            throw new MojoFailureException("Error during project build: " + result.getExitCode());
        }
    }
    if (!p.isExecutionRoot() || p.getModel().getModules().isEmpty()) {
        File gpgTempDirectory = null;
        try {
            URL gpgTempDirectoryURL = new URL(request.getProperties().get("url").toString());
            gpgTempDirectory = new File(gpgTempDirectoryURL.getFile());
            File pomGpgSignature = new File(gpgTempDirectory,
                    p.getGroupId().replaceAll("\\.", "/") + "/" + p.getArtifactId() + "/" + p.getVersion() + "/"
                            + p.getArtifactId() + "-" + p.getVersion() + ".pom.asc");
            File gpgSignatureDest = new File(p.getBuild().getDirectory(), pomGpgSignature.getName());
            pomGpgSignature = new File(p.getFile().getParentFile(), "pom.xml.asc");
            org.apache.commons.io.FileUtils.copyFile(pomGpgSignature, gpgSignatureDest);
        } finally {
            if (gpgTempDirectory != null && gpgTempDirectory.exists()) {
                org.apache.commons.io.FileUtils.deleteDirectory(gpgTempDirectory);
            }
            File ascFile = new File(p.getFile().getParentFile(), "pom.xml.asc");
            if (ascFile.exists()) {
                ascFile.delete();
            }
        }
    }
}

From source file:io.teecube.t3.GenericReplacerMojo.java

License:Apache License

private InvocationRequest getInvocationRequest(MavenProject p) throws MojoExecutionException, IOException {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setPomFile(p.getFile());/*from   w ww . j  av  a 2  s .  c  o m*/
    if (p.isExecutionRoot() && !p.getModel().getModules().isEmpty()) {
        request.setGoals(Lists.newArrayList("gpg:sign"));
    } else {
        File gpgDirectory = Files.createTempDir();
        request.setGoals(Lists.newArrayList("gpg:sign-and-deploy-file"));
        Properties properties = new Properties();
        properties.put("file", p.getFile().getAbsolutePath());
        //         properties.put("pomFile", p.getFile().getAbsolutePath());
        properties.put("groupId", p.getGroupId());
        properties.put("artifactId", p.getArtifactId());
        properties.put("version", p.getVersion());
        properties.put("repositoryId", "null");
        properties.put("url", gpgDirectory.toURI().toURL().toString());
        request.setProperties(properties);
    }
    request.setRecursive(false);
    this.tempSettingsFile = createAndSetTempSettings(request);
    return request;
}

From source file:licenseUtil.LicensingObject.java

License:Apache License

LicensingObject(MavenProject project, String includingProject, String version,
        Map<String, String> licenseUrlFileMappings) {
    super();//from   www . j  a  v a2  s  . c o  m
    put(ColumnHeader.ARTIFACT_ID.value(), project.getArtifactId());
    put(ColumnHeader.GROUP_ID.value(), project.getGroupId());
    put(ColumnHeader.VERSION.value(), project.getVersion());

    if (project.getLicenses() != null && !project.getLicenses().isEmpty()) {
        String licenseNames = "";
        String licenseUrls = "";
        String licenseComments = "";
        String licenseFN = null;
        int i = 0;
        for (License license : project.getLicenses()) {
            if (license.getUrl() != null && licenseFN == null)
                // remove protocol and lookup license url
                licenseFN = Utils.getValue(licenseUrlFileMappings,
                        license.getUrl().replaceFirst("^[^:]+://", ""));
            if (i++ > 0) {
                licenseNames += multiEntriesSeparator;
                licenseUrls += multiEntriesSeparator;
                licenseComments += multiEntriesSeparator;
            }
            licenseNames += license.getName();
            if (!Strings.isNullOrEmpty(license.getUrl()))
                licenseUrls += license.getUrl();
            if (!Strings.isNullOrEmpty(license.getComments()))
                licenseComments += license.getComments();
        }
        if (!Strings.isNullOrEmpty(licenseFN))
            put(ColumnHeader.LICENSE_TEMPLATE.value(), licenseFN);
        if (!Strings.isNullOrEmpty(licenseNames))
            put(ColumnHeader.LICENSE_NAMES.value(), licenseNames);
        if (!Strings.isNullOrEmpty(licenseUrls))
            put(ColumnHeader.LICENSE_URLS.value(), licenseUrls);
        if (!Strings.isNullOrEmpty(licenseComments))
            put(ColumnHeader.LICENSE_COMMENTS.value(), licenseComments);
    }
    put(includingProject, version);
    //clean();
}

From source file:me.gladwell.eclipse.m2e.android.project.AdtEclipseAndroidWorkspace.java

License:Open Source License

public EclipseAndroidProject findOpenWorkspaceDependency(Dependency dependency) {
    for (IProject project : workspace.getRoot().getProjects()) {
        if (!project.isOpen()) {
            continue;
        }//w w  w . j a v a 2 s .c  o m
        EclipseAndroidProject androidProject = projectFactory.createAndroidProject(project);
        if (androidProject.isMavenised()) {
            MavenProject mavenProject;
            try {
                mavenProject = mavenModelManager.readMavenProject(androidProject.getPom(), null);
            } catch (CoreException e) {
                throw new ProjectConfigurationException(e);
            }

            if (StringUtils.equals(dependency.getName(), project.getName())
                    && StringUtils.equals(dependency.getGroup(), mavenProject.getGroupId())
                    && dependency.getVersion().equals(mavenProject.getVersion())) {
                return androidProject;
            }
        }
    }

    throw new DependencyNotFoundInWorkspace(dependency);
}

From source file:me.gladwell.eclipse.m2e.android.project.EclipseAndroidWorkspace.java

License:Open Source License

public IDEAndroidProject findOpenWorkspaceDependency(Dependency dependency) {
    for (IProject project : workspace.getRoot().getProjects()) {
        if (!project.isOpen()) {
            continue;
        }//w ww  .  j a  v a  2  s  .  c om
        IDEAndroidProject androidProject = projectFactory.createAndroidProject(project);
        if (androidProject.isMavenised()) {
            MavenProject mavenProject;
            try {
                mavenProject = mavenModelManager.readMavenProject(androidProject.getPom(), null);
            } catch (CoreException e) {
                throw new ProjectConfigurationException(e);
            }

            if (StringUtils.equals(dependency.getName(), project.getName())
                    && StringUtils.equals(dependency.getGroup(), mavenProject.getGroupId())
                    && dependency.getVersion().equals(mavenProject.getVersion())) {
                return androidProject;
            }
        }
    }

    throw new DependencyNotFoundInWorkspace(dependency);
}