You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Stephane Nicoll <st...@gmail.com> on 2007/08/03 13:56:02 UTC

Tools for merging and managing repositories

Hi,

I was wondering if anyone was working on repository tools. What I need
is the ability to merge a "detached" repository (i.e. a repository
created by the assembly plugin) with either the local repository or a
remote repository.

I know the maven repositorytools plugin does that but it's in the
codehaus sandbox and nobody seems to be working on it anymore.

thoughts?

Thanks,
Stéphane

-- 
Large Systems Suck: This rule is 100% transitive. If you build one,
you suck" -- S.Yegge

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Tools for merging and managing repositories

Posted by Tom Huybrechts <to...@gmail.com>.
The repositorytools tried to do too many things at once, and the
result was that it almost didn't work at all :)

This simple code should get you somewhere - it is an implementation of
a release plugin phase, but you can transform it into a mojo. It will
take a remote repository on the local filesystem, and deploy all the
artifacts in it.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.deployer.ArtifactDeployer;
import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.release.ReleaseExecutionException;
import org.apache.maven.shared.release.ReleaseFailureException;
import org.apache.maven.shared.release.ReleaseResult;
import org.apache.maven.shared.release.config.ReleaseDescriptor;
import org.apache.maven.shared.release.phase.AbstractReleasePhase;
import org.apache.maven.wagon.repository.Repository;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
 * @plexus.component role="org.apache.maven.shared.release.phase.ReleasePhase"
 *                   role-hint="deploy-repository"
 *
 */
public class DeployRepositoryPhase extends AbstractReleasePhase {

    private ArtifactRepository localRepository;
    private ArtifactRepository stagingRepository;
    private ArtifactRepository deploymentRepository;

    /**
     * @todo replace this with a real deployment that also works remote.
     */
    public ReleaseResult execute(ReleaseDescriptor releaseDescriptor,
	    Settings settings, List reactorProjects)
	    throws ReleaseExecutionException, ReleaseFailureException {
	ReleaseResult result = new ReleaseResult();

	try {
	    deploymentRepository = RepositoryUtil
		    .createRepository(releaseDescriptor
			    .getDeploymentRepository());
	    stagingRepository = RepositoryUtil
		    .createRepository(releaseDescriptor
			    .getTempDeploymentRepository());

	    localRepository = new DefaultArtifactRepository("local", new File(
		    releaseDescriptor.getLocalRepository()).toURL().toString(),
		    new DefaultRepositoryLayout());

	    for (Artifact a : getArtifacts(stagingRepository.getBasedir()))
		deployArtifact(a.getGroupId(), a.getArtifactId(), a
			.getVersion());

	    result.setResultCode(ReleaseResult.SUCCESS);
	} catch (Exception e) {
	    logError(result, "Error while releasing.");
	    result.setResultCode(ReleaseResult.ERROR);
	}
	return result;

    }

    private List<Artifact> getArtifacts(String basedir) {
	DirectoryScanner ds = new DirectoryScanner();
	ds.setIncludes(new String[] { "**/*.pom" });
	ds.setBasedir(basedir);
	ds.scan();
	String[] includedFiles = ds.getIncludedFiles();
	List<Artifact> result = new ArrayList<Artifact>();
	for (String s : includedFiles) {
	    s = s.substring(0, s.lastIndexOf(File.separatorChar));
	    int i = s.lastIndexOf(File.separatorChar);
	    String version = s.substring(i + 1);
	    s = s.substring(0, i);
	    i = s.lastIndexOf(File.separatorChar);
	    String artifactId = s.substring(i + 1);
	    String groupId = s.substring(0, i).replace(File.separatorChar, '.');
	    Artifact artifact = artifactFactory.createArtifactWithClassifier(
		    groupId, artifactId, version, "pom", null);
	    result.add(artifact);
	    System.out.printf("%s %s %s\n", groupId, artifactId, version);
	}
	return result;

    }

    public ReleaseResult simulate(ReleaseDescriptor releaseDescriptor,
	    Settings settings, List reactorProjects)
	    throws ReleaseExecutionException, ReleaseFailureException {
	ReleaseResult result = new ReleaseResult();
	result.setResultCode(ReleaseResult.ERROR);
	return result;
    }

    /**
     * @plexus.requirement
     */
    private ArtifactFactory artifactFactory;

    /**
     * @plexus.requirement
     */
    private ArtifactDeployer artifactDeployer;

    public void deployArtifact(String groupId, String artifactId,
String version)
	    throws ArtifactDeploymentException, ReleaseExecutionException {

	Artifact pomArtifact = artifactFactory.createArtifactWithClassifier(
		groupId, artifactId, version, "pom", null);
	String basedir = ((Repository) stagingRepository).getBasedir();
	File pomFile = new File(basedir, stagingRepository.pathOf(pomArtifact));
	Model model = readModel(pomFile);

	Artifact artifact = artifactFactory.createArtifactWithClassifier(
		groupId, artifactId, version, model.getPackaging(), null);
	File file = new File(basedir, stagingRepository.pathOf(artifact));

	ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact,
		pomFile);
	artifact.addMetadata(metadata);
	artifactDeployer.deploy(file, artifact, deploymentRepository,
		localRepository);

	DirectoryScanner ds = new DirectoryScanner();
	ds.setBasedir(file.getParent());
	ds.setExcludes(new String[] { "*.md5", "*.sha1", pomFile.getName(),
		file.getName() });
	ds.scan();
	Pattern pattern = Pattern.compile(artifactId + "-" + version
		+ "-(.*)\\.(.*)");
	for (String s : ds.getIncludedFiles()) {
	    Matcher m = pattern.matcher(s);
	    if (!m.matches()) {
		throw new IllegalArgumentException("File does not match!");
	    }
	    String classifier = m.group(1);
	    String type = m.group(2);
	    Artifact attached = artifactFactory.createArtifactWithClassifier(
		    groupId, artifactId, version, type, classifier);
	    artifactDeployer.deploy(new File(file.getParent(), s), attached,
		    deploymentRepository, localRepository);
	}

    }

    /**
     * Extract the Model from the specified file.
     *
     * @param pomFile
     * @return
     * @throws MojoExecutionException
     *                 if the file doesn't exist of cannot be read.
     */
    protected Model readModel(File pomFile) throws ReleaseExecutionException {

	if (!pomFile.exists()) {
	    throw new ReleaseExecutionException(
		    "Specified pomFile does not exist");
	}

	Reader reader = null;
	try {
	    reader = new FileReader(pomFile);
	    MavenXpp3Reader modelReader = new MavenXpp3Reader();
	    return modelReader.read(reader);
	} catch (FileNotFoundException e) {
	    throw new ReleaseExecutionException(
		    "Error reading specified POM file: " + e.getMessage(), e);
	} catch (IOException e) {
	    throw new ReleaseExecutionException(
		    "Error reading specified POM file: " + e.getMessage(), e);
	} catch (XmlPullParserException e) {
	    throw new ReleaseExecutionException(
		    "Error reading specified POM file: " + e.getMessage(), e);
	} finally {
	    IOUtil.close(reader);
	}
    }

}



On 8/3/07, Stephane Nicoll <st...@gmail.com> wrote:
> Hi,
>
> I was wondering if anyone was working on repository tools. What I need
> is the ability to merge a "detached" repository (i.e. a repository
> created by the assembly plugin) with either the local repository or a
> remote repository.
>
> I know the maven repositorytools plugin does that but it's in the
> codehaus sandbox and nobody seems to be working on it anymore.
>
> thoughts?
>
> Thanks,
> Stéphane
>
> --
> Large Systems Suck: This rule is 100% transitive. If you build one,
> you suck" -- S.Yegge
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org