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

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

Introduction

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

Prototype

public Scm getScm() 

Source Link

Usage

From source file:aQute.bnd.maven.reporter.plugin.entries.mavenproject.CommonInfoPlugin.java

private ScmDTO extractScm(MavenProject project) {
    if (project.getScm() != null) {
        ScmDTO scm = new ScmDTO();
        scm.connection = project.getScm().getConnection();
        scm.developerConnection = project.getScm().getDeveloperConnection();
        scm.url = project.getScm().getUrl();
        scm.tag = project.getScm().getTag();
        return scm;
    } else {//w w  w .  ja  va  2s  .  co m
        return null;
    }
}

From source file:com.effectivemaven.centrepoint.maven.repository.CentralRepositoryService.java

License:Apache License

public Project retrieveProject(String groupId, String artifactId, String version) throws RepositoryException {
    // get the project from the repository
    Artifact artifact = artifactFactory.createProjectArtifact(groupId, artifactId, version);
    MavenProject mavenProject;
    try {/*  w  w  w.  j  a va  2 s . com*/
        mavenProject = projectBuilder.buildFromRepository(artifact, Collections.singletonList(repository),
                localRepository);
    } catch (ProjectBuildingException e) {
        throw new RepositoryException(e.getMessage(), e);
    }

    // populate the Centrepoint model from the Maven project
    Project project = new Project();
    project.setId(MavenCoordinates.constructProjectId(groupId, artifactId));
    project.setVersion(version);

    MavenCoordinates coordinates = new MavenCoordinates();
    coordinates.setGroupId(groupId);
    coordinates.setArtifactId(artifactId);
    project.addExtensionModel(coordinates);

    project.setDescription(mavenProject.getDescription());
    project.setName(mavenProject.getName());
    if (mavenProject.getCiManagement() != null) {
        project.setCiManagementUrl(mavenProject.getCiManagement().getUrl());
    }
    if (mavenProject.getIssueManagement() != null) {
        project.setIssueTrackerUrl(mavenProject.getIssueManagement().getUrl());
    }
    if (mavenProject.getScm() != null) {
        project.setScmUrl(mavenProject.getScm().getUrl());
    }
    project.setUrl(mavenProject.getUrl());

    DistributionManagement distMgmt = mavenProject.getDistributionManagement();
    if (distMgmt != null) {
        project.setRepositoryUrl(distMgmt.getRepository().getUrl());
        project.setSnapshotRepositoryUrl(distMgmt.getSnapshotRepository().getUrl());
    }

    return project;
}

From source file:com.github.maven.plugins.core.RepositoryUtils.java

License:Open Source License

/**
 * Get repository/*from  w  w  w . j  a  v a  2  s. c o  m*/
 * 
 * @param project
 * @param owner
 * @param name
 * 
 * @return repository id or null if none configured
 */
public static RepositoryId getRepository(final MavenProject project, final String owner, final String name) {
    // Use owner and name if specified
    if (!StringUtils.isEmpty(owner, name))
        return RepositoryId.create(owner, name);

    if (project == null)
        return null;

    RepositoryId repo = null;
    // Extract repository from SCM URLs first if present
    final Scm scm = project.getScm();
    if (scm != null) {
        repo = RepositoryId.createFromUrl(scm.getUrl());
        if (repo == null)
            repo = extractRepositoryFromScmUrl(scm.getConnection());
        if (repo == null)
            repo = extractRepositoryFromScmUrl(scm.getDeveloperConnection());
    }

    // Check project URL last
    if (repo == null)
        repo = RepositoryId.createFromUrl(project.getUrl());

    return repo;
}

From source file:com.nesscomputing.mojo.numbers.macros.ScmMacro.java

License:Apache License

public ScmRepository getScmRepository(final MavenProject project, final Settings settings,
        final Properties props) throws ScmException {
    final Scm scm = project.getScm();

    Preconditions.checkState(scm != null, "No scm element in the project found!");

    ScmRepository repository;/*from  w ww .j  ava 2s  .c o m*/

    try {
        repository = scmManager.makeScmRepository(getConnectionUrl(scm, props));

        if (repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost) {
            final ScmProviderRepositoryWithHost repo = (ScmProviderRepositoryWithHost) repository
                    .getProviderRepository();
            final Server server = settings.getServer(repo.getHost());

            if (server != null) {
                repo.setUser(server.getUsername());
                repo.setPassword(server.getPassword());
                repo.setPrivateKey(server.getPrivateKey());
                repo.setPassphrase(server.getPassphrase());
            }
        }
    } catch (final ScmRepositoryException e) {
        if (!e.getValidationMessages().isEmpty()) {
            for (final String msg : e.getValidationMessages()) {
                LOG.error(msg);
            }
        }

        throw new ScmException("Can't load the scm provider.", e);
    } catch (final Exception e) {
        throw new ScmException("Can't load the scm provider.", e);
    }

    return repository;
}

From source file:io.fabric8.maven.enricher.standard.MavenScmEnricher.java

License:Apache License

@Override
public Map<String, String> getAnnotations(Kind kind) {
    Map<String, String> annotations = new HashMap<>();
    if (kind.isController() || kind == Kind.SERVICE) {
        MavenProject rootProject = getProject();
        if (hasScm(rootProject)) {
            Scm scm = rootProject.getScm();
            String connectionUrl = scm.getConnection();
            String devConnectionUrl = scm.getDeveloperConnection();
            String url = scm.getUrl();
            String tag = scm.getTag();

            if (StringUtils.isNotEmpty(connectionUrl)) {
                annotations.put(SCM_CONNECTION, connectionUrl);
            }//from  w  w w .  j  a v  a2 s  . c  o m
            if (StringUtils.isNotEmpty(devConnectionUrl)) {
                annotations.put(SCM_DEVELOPER_CONNECTION, devConnectionUrl);
            }
            if (StringUtils.isNotEmpty(tag)) {
                annotations.put(SCM_TAG, tag);
            }
            if (StringUtils.isNotEmpty(url)) {
                annotations.put(SCM_URL, url);
            }
        }
    }
    return annotations;
}

From source file:io.fabric8.maven.enricher.standard.MavenScmEnricher.java

License:Apache License

private boolean hasScm(MavenProject project) {
    return project.getScm() != null;
}

From source file:io.siddhi.doc.gen.core.MkdocsGitHubPagesDeployMojo.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    // Finding the root maven project
    MavenProject rootMavenProject = mavenProject;
    while (rootMavenProject.getParent() != null && rootMavenProject.getParent().getBasedir() != null) {
        rootMavenProject = rootMavenProject.getParent();
    }/*  w w w  . ja  va  2  s.c om*/

    // Setting the relevant modules target directory if not set by user
    String moduleTargetPath;
    if (moduleTargetDirectory != null) {
        moduleTargetPath = moduleTargetDirectory.getAbsolutePath();
    } else {
        moduleTargetPath = mavenProject.getBuild().getDirectory();
    }

    // Setting the mkdocs config file path if not set by user
    if (mkdocsConfigFile == null) {
        mkdocsConfigFile = new File(rootMavenProject.getBasedir() + File.separator
                + Constants.MKDOCS_CONFIG_FILE_NAME + Constants.YAML_FILE_EXTENSION);
    }

    // Setting the documentation output directory if not set by user
    String docGenBasePath;
    if (docGenBaseDirectory != null) {
        docGenBasePath = docGenBaseDirectory.getAbsolutePath();
    } else {
        docGenBasePath = rootMavenProject.getBasedir() + File.separator + Constants.DOCS_DIRECTORY;
    }

    // Setting the home page file name if not set by user
    File homePageFile;
    if (homePageFileName == null) {
        homePageFile = new File(docGenBasePath + File.separator + Constants.HOMEPAGE_FILE_NAME
                + Constants.MARKDOWN_FILE_EXTENSION);
    } else {
        homePageFile = new File(docGenBasePath + File.separator + homePageFileName);
    }

    // Setting the readme file name if not set by user
    if (readmeFile == null) {
        readmeFile = new File(rootMavenProject.getBasedir() + File.separator + Constants.README_FILE_NAME
                + Constants.MARKDOWN_FILE_EXTENSION);
    }

    // Setting the home page template file path if not set by user
    if (homePageTemplateFile == null) {
        homePageTemplateFile = new File(rootMavenProject.getBasedir() + File.separator
                + Constants.README_FILE_NAME + Constants.MARKDOWN_FILE_EXTENSION);
    }

    // Retrieving metadata
    List<NamespaceMetaData> namespaceMetaDataList;
    try {
        namespaceMetaDataList = DocumentationUtils.getExtensionMetaData(moduleTargetPath,
                mavenProject.getRuntimeClasspathElements(), getLog());
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoFailureException("Unable to resolve dependencies of the project", e);
    }

    // Generating the documentation
    if (namespaceMetaDataList.size() > 0) {
        DocumentationUtils.generateDocumentation(namespaceMetaDataList, docGenBasePath,
                mavenProject.getVersion(), getLog());
        DocumentationUtils.updateHeadingsInMarkdownFile(homePageTemplateFile, homePageFile,
                rootMavenProject.getArtifactId(), mavenProject.getVersion(), namespaceMetaDataList);
        DocumentationUtils.updateHeadingsInMarkdownFile(readmeFile, readmeFile,
                rootMavenProject.getArtifactId(), mavenProject.getVersion(), namespaceMetaDataList);
    }

    // Delete snapshot files
    DocumentationUtils.removeSnapshotAPIDocs(mkdocsConfigFile, docGenBasePath, getLog());

    // Updating the links in the home page to the mkdocs config
    try {
        updateAPIPagesInMkdocsConfig(mkdocsConfigFile, docGenBasePath);
    } catch (FileNotFoundException e) {
        getLog().warn("Unable to find mkdocs configuration file: " + mkdocsConfigFile.getAbsolutePath()
                + ". Mkdocs configuration file not updated.");
    }
    // Deploying the documentation
    if (DocumentationUtils.generateMkdocsSite(mkdocsConfigFile, getLog())) {
        // Creating the credential provider fot Git
        String scmUsername = System.getenv(Constants.SYSTEM_PROPERTY_SCM_USERNAME_KEY);
        String scmPassword = System.getenv(Constants.SYSTEM_PROPERTY_SCM_PASSWORD_KEY);

        if (scmUsername == null && scmPassword == null) {
            getLog().info("SCM_USERNAME and SCM_PASSWORD not defined!");
        }
        String url = null;
        Scm scm = rootMavenProject.getScm();
        if (scm != null) {
            url = scm.getUrl();
        }
        // Deploying documentation
        DocumentationUtils.updateDocumentationOnGitHub(docGenBasePath, mkdocsConfigFile, readmeFile,
                mavenProject.getVersion(), getLog());
        DocumentationUtils.deployMkdocsOnGitHubPages(mavenProject.getVersion(), rootMavenProject.getBasedir(),
                url, scmUsername, scmPassword, getLog());
    } else {
        getLog().warn("Unable to generate documentation. Skipping documentation deployment.");
    }
}

From source file:io.sundr.maven.GenerateBomMojo.java

License:Apache License

/**
 * Returns the model of the {@link org.apache.maven.project.MavenProject} to generate.
 * This is a trimmed down version and contains just the stuff that need to go into the bom.
 *
 * @param project The source {@link org.apache.maven.project.MavenProject}.
 * @param config  The {@link io.sundr.maven.BomConfig}.
 * @return The build {@link org.apache.maven.project.MavenProject}.
 *///from w w w .j  av  a 2  s .  c om
private static MavenProject toGenerate(MavenProject project, BomConfig config,
        Collection<Dependency> dependencies, Set<Artifact> plugins) {
    MavenProject toGenerate = project.clone();
    toGenerate.setGroupId(project.getGroupId());
    toGenerate.setArtifactId(config.getArtifactId());
    toGenerate.setVersion(project.getVersion());
    toGenerate.setPackaging("pom");
    toGenerate.setName(config.getName());
    toGenerate.setDescription(config.getDescription());

    toGenerate.setUrl(project.getUrl());
    toGenerate.setLicenses(project.getLicenses());
    toGenerate.setScm(project.getScm());
    toGenerate.setDevelopers(project.getDevelopers());

    toGenerate.getModel().setDependencyManagement(new DependencyManagement());
    for (Dependency dependency : dependencies) {
        toGenerate.getDependencyManagement().addDependency(dependency);
    }

    toGenerate.getModel().setBuild(new Build());
    if (!plugins.isEmpty()) {
        toGenerate.getModel().setBuild(new Build());
        toGenerate.getModel().getBuild().setPluginManagement(new PluginManagement());
        for (Artifact artifact : plugins) {
            toGenerate.getPluginManagement().addPlugin(toPlugin(artifact));
        }
    }

    return toGenerate;
}

From source file:io.sundr.maven.GenerateBomMojo.java

License:Apache License

/**
 * Returns the generated {@link org.apache.maven.project.MavenProject} to build.
 * This version of the project contains all the stuff needed for building (parents, profiles, properties etc).
 *
 * @param project The source {@link org.apache.maven.project.MavenProject}.
 * @param config  The {@link io.sundr.maven.BomConfig}.
 * @return The build {@link org.apache.maven.project.MavenProject}.
 *///from w  w  w . ja v  a 2  s .com
private static MavenProject toBuild(MavenProject project, BomConfig config) {
    File outputDir = new File(project.getBuild().getOutputDirectory());
    File bomDir = new File(outputDir, config.getArtifactId());
    File generatedBom = new File(bomDir, BOM_NAME);

    MavenProject toBuild = project.clone();
    //we want to avoid recursive "generate-bom".
    toBuild.setExecutionRoot(false);
    toBuild.setFile(generatedBom);
    toBuild.getModel().setPomFile(generatedBom);
    toBuild.setModelVersion(project.getModelVersion());

    toBuild.setArtifact(new DefaultArtifact(project.getGroupId(), config.getArtifactId(), project.getVersion(),
            project.getArtifact().getScope(), project.getArtifact().getType(),
            project.getArtifact().getClassifier(), project.getArtifact().getArtifactHandler()));

    toBuild.setParent(project.getParent());
    toBuild.getModel().setParent(project.getModel().getParent());

    toBuild.setGroupId(project.getGroupId());
    toBuild.setArtifactId(config.getArtifactId());
    toBuild.setVersion(project.getVersion());
    toBuild.setPackaging("pom");
    toBuild.setName(config.getName());
    toBuild.setDescription(config.getDescription());

    toBuild.setUrl(project.getUrl());
    toBuild.setLicenses(project.getLicenses());
    toBuild.setScm(project.getScm());
    toBuild.setDevelopers(project.getDevelopers());
    toBuild.setDistributionManagement(project.getDistributionManagement());
    toBuild.getModel().setProfiles(project.getModel().getProfiles());

    //We want to avoid having the generated stuff wiped.
    toBuild.getProperties().put("clean.skip", "true");
    toBuild.getModel().getBuild().setDirectory(bomDir.getAbsolutePath());
    toBuild.getModel().getBuild().setOutputDirectory(new File(bomDir, "target").getAbsolutePath());
    for (String key : config.getProperties().stringPropertyNames()) {
        toBuild.getProperties().put(key, config.getProperties().getProperty(key));
    }
    return toBuild;
}

From source file:net.java.jpatch.maven.common.AbstractPatchMojo.java

License:Apache License

/**
 * Check the SCM status of the project./*from w ww .  j  av  a  2s. co  m*/
 *
 * @param project the MAVEN project.
 * @param begin the SCM ID.
 * @param end the SCM ID.
 *
 * @return
 *
 * @throws MojoExecutionException if the method fails.
 */
private boolean checkSCM(MavenProject project, String revision, File diffDirectory)
        throws MojoExecutionException {

    getLog().info("Check SCM for the project " + project.getId());

    boolean result = false;
    boolean ignoreUnknown = true;

    DiffScmResult scmResult = null;
    try {
        ScmRepository repository = scmManager.makeScmRepository(project.getScm().getConnection());
        scmResult = scmManager.diff(repository, new ScmFileSet(project.getBasedir()), new ScmRevision(revision),
                null);
    } catch (Exception e) {
        getLog().error("Error check SCM status for project " + project.getId());
        throw new MojoExecutionException("Couldn't configure SCM repository: " + e.getLocalizedMessage(), e);
    }

    if (scmResult != null && scmResult.getChangedFiles() != null) {
        List<ScmFile> changedFiles = scmResult.getChangedFiles();
        for (ScmFile changedScmFile : changedFiles) {
            ScmFileStatus status = changedScmFile.getStatus();
            if (!status.isStatus()) {
                getLog().debug("Not a diff: " + status);
                continue;
            }
            if (ignoreUnknown && ScmFileStatus.UNKNOWN.equals(status)) {
                getLog().debug("Ignoring unknown");
                continue;
            }

            getLog().info(changedScmFile.getStatus().toString() + " " + changedScmFile.getPath());
            result = true;
        }

        if (result) {
            File diffFile = new File(diffDirectory, project.getArtifactId() + project.getVersion() + ".diff");
            try {
                getLog().info("Create new diff file: " + diffFile.getAbsolutePath());
                diffFile.createNewFile();
                FileUtils.fileWrite(diffFile.getAbsolutePath(), scmResult.getPatch());
            } catch (IOException ex) {
                Logger.getLogger(AbstractPatchMojo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    getLog().info("");
    return result;
}