com.greenpepper.maven.runner.resolver.CoordinatesResolver Maven / Gradle / Ivy
/**
* Copyright (c) 2009 Pyxis Technologies inc.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
* or see the FSF site: http://www.fsf.org.
*
* @author oaouattara
* @version $Id: $Id
*/
package com.greenpepper.maven.runner.resolver;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.cli.ConsoleDownloadMonitor;
import org.apache.maven.embedder.MavenEmbedder;
import org.apache.maven.embedder.MavenEmbedderLogger;
import org.apache.maven.model.Repository;
import org.apache.maven.project.MavenProject;
import com.greenpepper.maven.runner.resolver.ProjectFileResolver.MavenGAV;
import com.greenpepper.util.StringUtil;
public class CoordinatesResolver implements ProjectFileResolver.Resolver
{
// A Maven coordinates : http://maven.apache.org/pom.html#Maven_Coordinates
// groupId:artifactId[:packaging][:classifier]:version
// packaging = [pom, jar, maven-plugin, ejb, war, ear, rar, par]
private static final String REGEX = "(.*?):(.*?):(.*?:)?(.*?:)?(.*?)";
private final Pattern coordinatesPattern = Pattern.compile(REGEX);
private final MavenEmbedder embedder;
private final MavenEmbedderLogger logger;
private MavenGAV mavenGAV;
/**
* Constructor for CoordinatesResolver.
*
* @param embedder a {@link org.apache.maven.embedder.MavenEmbedder} object.
* @param logger a {@link org.apache.maven.embedder.MavenEmbedderLogger} object.
*/
public CoordinatesResolver(MavenEmbedder embedder, MavenEmbedderLogger logger)
{
this.embedder = embedder;
this.logger = logger;
}
/** {@inheritDoc} */
public final boolean canResolve(String value)
{
return !StringUtil.isEmpty( value ) && coordinatesPattern.matcher( value ).find();
}
/** {@inheritDoc} */
public final File resolve(String value) throws Exception
{
MavenGAV mavenGAV = resolveCoordinates( value );
logger.info( String.format( "Using maven coordinates '%s' as project", mavenGAV ) );
resolveTemporaryProjectFile();
return resolveProjectFile();
}
/** {@inheritDoc} */
@Override
public MavenGAV getMavenGAV() {
return mavenGAV;
}
private MavenGAV resolveCoordinates(String value) {
Matcher matcher = coordinatesPattern.matcher(value);
if (matcher.matches()) {
String groupId = matcher.group(1);
String artifactId = matcher.group(2);
String packaging = StringUtils.strip(matcher.group(3), ":");
String classifier = StringUtils.strip(matcher.group(4), ":");
String version = matcher.group(5);
mavenGAV = new MavenGAV(groupId, artifactId, version);
mavenGAV.setPackaging(packaging);
mavenGAV.setClassifier(classifier);
logger.info(String.format("Using maven coordinates '%s' as project", mavenGAV));
return mavenGAV;
}
throw new IllegalArgumentException("The value is not resolveable by the pattern: " + REGEX);
}
private void resolveTemporaryProjectFile() throws Exception
{
File tmpProjectFile = createTemporaryPomFile( mavenGAV.getGroupId(), mavenGAV.getArtifactId(), mavenGAV.getVersion());
MavenProject tmpProject = embedder.readProjectWithDependencies( tmpProjectFile, new ConsoleDownloadMonitor() );
Artifact targetedArtifact = (Artifact) tmpProject.getDependencyArtifacts().iterator().next();
mavenGAV.setVersion(targetedArtifact.getVersion());
}
private File resolveProjectFile() throws ArtifactResolutionException, ArtifactNotFoundException
{
Artifact artifact = embedder.createArtifact( mavenGAV.getGroupId(), mavenGAV.getArtifactId(), mavenGAV.getVersion(), "runtime", "pom" );
embedder.resolve( artifact, new ArrayList(), embedder.getLocalRepository() );
return artifact.getFile();
}
private File createTemporaryPomFile(String groupId, String artifactId, String version) throws IOException
{
File tempPomFile = File.createTempFile( "pom-", ".xml" );
FileWriter pomWriter = null;
try
{
pomWriter = new FileWriter( tempPomFile );
pomWriter.write( toPOM( groupId, artifactId, version ) );
}
finally
{
tempPomFile.deleteOnExit();
if (pomWriter != null)
{
pomWriter.close();
}
}
return tempPomFile;
}
private String toPOM(String groupId, String artifactId, String version)
{
StringBuilder pom = new StringBuilder();
pom.append( "\n" );
pom.append( " 4.0.0 \n" );
pom.append( " " ).append( "greenpepper" ).append( " \n" );
pom.append( " " ).append( "greenpepper-maven-runner-resolver" ).append( " \n" );
pom.append( " " ).append( "1.0" ).append( " \n" );
pom.append( " " ).append( "pom" ).append( " \n" );
pom.append( " \n" );
pom.append( " \n" );
pom.append( " " ).append( groupId ).append( " \n" );
pom.append( " " ).append( artifactId ).append( " \n" );
pom.append( " " ).append( version ).append( " \n" );
pom.append( " \n" );
pom.append( " \n" );
pom.append( " " );
return pom.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy