private String checkoutSourcesFromSCM()

in src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java [1452:1594]


    private String checkoutSourcesFromSCM(
            final ApisJarContext ctx, final ArtifactInfo info, final Artifact sourceArtifact)
            throws MojoExecutionException {
        // fallback to Artifacts SCM metadata first
        String connection = sourceArtifact.getMetadata().get(ApisUtil.SCM_LOCATION);
        String tag = sourceArtifact.getMetadata().get(ApisUtil.SCM_TAG);

        // Artifacts SCM metadata may not available or are an override, let's fallback
        // to the POM
        getLog().debug("Falling back to SCM checkout...");
        final Model pomModel = getArtifactPom(ctx, sourceArtifact.getId());
        getLog().debug("Processing SCM info from pom...");

        final Scm scm = pomModel.getScm();
        if (scm != null) {
            if (connection == null) {
                connection = scm.getConnection();
            }
            if (tag == null) {
                tag = scm.getTag();
                // Maven uses "HEAD" as default value
                if ("HEAD".equals(tag)) {
                    tag = null;
                }
            }
        }

        if (connection == null) {
            getLog().warn("Ignoring sources for artifact "
                    + sourceArtifact.getId().toMvnId() + " : SCM not defined in "
                    + sourceArtifact.getId().toMvnId() + " bundle neither in " + pomModel.getId() + " POM file.");
            return null;
        }

        try {
            ScmRepository repository = scmManager.makeScmRepository(connection);

            ScmVersion scmVersion = null;
            if (tag != null) {
                scmVersion = new ScmTag(tag);
            }

            File basedir = new File(
                    ctx.getCheckedOutSourcesDir(), sourceArtifact.getId().toMvnName());
            if (basedir.exists()) {
                getLog().debug("Source checkout directory " + basedir + " already exists");
            } else {
                getLog().debug("Checking out source to directory " + basedir);
                basedir.mkdirs();
                ScmFileSet fileSet = new ScmFileSet(basedir);

                CheckOutScmResult result = null;
                try {
                    if (scmVersion == null) {
                        result = scmManager.checkOut(repository, fileSet, true);
                    } else {
                        result = scmManager.checkOut(repository, fileSet, scmVersion, true);
                    }
                } catch (ScmException se) {
                    throw new MojoExecutionException(
                            "An error occurred while checking sources from " + repository + " for artifact "
                                    + sourceArtifact.getId().toMvnId() + " model",
                            se);
                }

                if (!result.isSuccess()) {
                    getLog().warn("Ignoring sources for artifact "
                            + sourceArtifact.getId().toMvnId()
                            + " : An error occurred while checking out sources from " + connection + ": "
                            + result.getProviderMessage());
                    return null;
                }
            }

            // retrieve the exact pom location to detect the bundle path in the repo
            DirectoryScanner pomScanner = new DirectoryScanner();
            pomScanner.setBasedir(basedir);
            pomScanner.setIncludes("**/pom.xml");
            pomScanner.scan();
            for (String pomFileLocation : pomScanner.getIncludedFiles()) {
                final File pomFile = new File(basedir, pomFileLocation);
                final Model model = modelBuilder
                        .buildRawModel(pomFile, ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL, false)
                        .get();

                if (sourceArtifact.getId().getArtifactId().equals(model.getArtifactId())) {
                    basedir = pomFile.getParentFile();
                    break;
                }
            }

            // copy all interested sources to the proper location
            File javaSources = new File(basedir, "src/main/java");
            if (!javaSources.exists()) { // old modules could still use src/java
                javaSources = new File(basedir, "src/java");

                // there could be just resources artifacts
                if (!javaSources.exists()) {
                    getLog().warn("Ignoring sources for artifact "
                            + sourceArtifact.getId().toMvnId() + " : SCM checkout for "
                            + sourceArtifact.getId().toMvnId() + " does not contain any source.");
                    return null;
                }
            }

            final File sourceDirectory =
                    new File(ctx.getDeflatedSourcesDir(), info.getId().toMvnName());
            info.setSourceDirectory(sourceDirectory);
            sourceDirectory.mkdir();

            final DirectoryScanner directoryScanner = new DirectoryScanner();
            directoryScanner.setBasedir(javaSources);
            directoryScanner.setIncludes(info.getUsedExportedPackageIncludes());
            directoryScanner.scan();

            for (String file : directoryScanner.getIncludedFiles()) {
                final File source = new File(javaSources, file);
                final File destination = new File(sourceDirectory, file);
                destination.getParentFile().mkdirs();
                try {
                    FileUtils.copyFile(source, destination);
                } catch (IOException e) {
                    throw new MojoExecutionException(
                            "An error occurred while copying sources from " + source + " to " + destination, e);
                }
            }

            return tag == null ? connection : connection.concat("@").concat(tag);
        } catch (ScmRepositoryException se) {
            throw new MojoExecutionException(
                    "An error occurred while reading SCM from " + connection + " connection for bundle "
                            + sourceArtifact.getId(),
                    se);
        } catch (NoSuchScmProviderException nsspe) {
            getLog().warn(
                            "Ignoring sources for artifact "
                                    + sourceArtifact.getId().toMvnId()
                                    + " : bundle points to an SCM connection " + connection
                                    + " which does not specify a valid or supported SCM provider",
                            nsspe);
            return null;
        }
    }