use of org.apache.maven.project.ProjectBuildingRequest in project maven-dependency-plugin by apache.
the class GetMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (isSkip()) {
getLog().info("Skipping plugin execution");
return;
}
if (coordinate.getArtifactId() == null && artifact == null) {
throw new MojoFailureException("You must specify an artifact, " + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0");
}
if (artifact != null) {
String[] tokens = StringUtils.split(artifact, ":");
if (tokens.length < 3 || tokens.length > 5) {
throw new MojoFailureException("Invalid artifact, you must specify " + "groupId:artifactId:version[:packaging[:classifier]] " + artifact);
}
coordinate.setGroupId(tokens[0]);
coordinate.setArtifactId(tokens[1]);
coordinate.setVersion(tokens[2]);
if (tokens.length >= 4) {
coordinate.setType(tokens[3]);
}
if (tokens.length == 5) {
coordinate.setClassifier(tokens[4]);
}
}
ArtifactRepositoryPolicy always = new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();
if (pomRemoteRepositories != null) {
repoList.addAll(pomRemoteRepositories);
}
if (remoteRepositories != null) {
// Use the same format as in the deploy plugin id::layout::url
List<String> repos = Arrays.asList(StringUtils.split(remoteRepositories, ","));
for (String repo : repos) {
repoList.add(parseRepository(repo, always));
}
}
try {
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setRemoteRepositories(repoList);
if (transitive) {
getLog().info("Resolving " + coordinate + " with transitive dependencies");
dependencyResolver.resolveDependencies(buildingRequest, coordinate, null);
} else {
getLog().info("Resolving " + coordinate);
artifactResolver.resolveArtifact(buildingRequest, toArtifactCoordinate(coordinate));
}
} catch (ArtifactResolverException e) {
throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
} catch (DependencyResolverException e) {
throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
}
}
use of org.apache.maven.project.ProjectBuildingRequest in project sts4 by spring-projects.
the class MavenBridge method compileAndGenerateJavadoc.
public MavenExecutionResult compileAndGenerateJavadoc(File pom) throws MavenException {
try {
MavenExecutionRequest request = createExecutionRequest();
lookup(MavenExecutionRequestPopulator.class).populateDefaults(request);
request.setPom(pom);
ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
configuration.setRepositorySession(createRepositorySession(request));
configuration.setResolveDependencies(true);
configuration.setResolveVersionRanges(true);
request.setGoals(Arrays.asList(new String[] { "compile", "javadoc:javadoc" }));
Properties userProperties = (Properties) request.getUserProperties().clone();
userProperties.put("show", "private");
request.setUserProperties(userProperties);
return lookup(Maven.class).execute(request);
} catch (MavenExecutionRequestPopulationException e) {
throw new MavenException(e);
}
}
use of org.apache.maven.project.ProjectBuildingRequest in project maven-archetype by apache.
the class DefaultArchetypeGenerationConfiguratorTest method setUp.
public void setUp() throws Exception {
super.setUp();
configurator = (DefaultArchetypeGenerationConfigurator) lookup(ArchetypeGenerationConfigurator.ROLE);
ProjectBuildingRequest buildingRequest = null;
MockControl control = MockControl.createControl(ArchetypeArtifactManager.class);
control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
ArchetypeArtifactManager manager = (ArchetypeArtifactManager) control.getMock();
manager.exists("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
control.setReturnValue(true);
manager.isFileSetArchetype("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
control.setReturnValue(false);
manager.isOldArchetype("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
control.setReturnValue(true);
manager.getOldArchetypeDescriptor("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
control.setReturnValue(new ArchetypeDescriptor());
control.replay();
configurator.setArchetypeArtifactManager(manager);
}
use of org.apache.maven.project.ProjectBuildingRequest in project maven-archetype by apache.
the class DefaultArchetypeGeneratorTest method createArchetypeGenerationRequest.
private ArchetypeGenerationRequest createArchetypeGenerationRequest(String project, Archetype archetype) {
outputDirectory = getBasedir() + "/target/test-classes/projects/" + project;
projectDirectory = new File(outputDirectory, "file-value");
ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
request.setLocalRepository(localRepository);
request.setArchetypeRepository(remoteRepository);
request.setOutputDirectory(outputDirectory);
request.setArchetypeGroupId(archetype.groupId);
request.setArchetypeArtifactId(archetype.artifactId);
request.setArchetypeVersion(archetype.version);
request.setGroupId("file-value");
request.setArtifactId("file-value");
request.setVersion("file-value");
request.setPackage("file.value.package");
request.setProperties(ADDITIONAL_PROPERTIES);
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
buildingRequest.setRepositorySession(repositorySession);
request.setProjectBuildingRequest(buildingRequest);
return request;
}
use of org.apache.maven.project.ProjectBuildingRequest in project maven-archetype by apache.
the class ArchetypeTest method testArchetype.
public void testArchetype() throws Exception {
FileUtils.deleteDirectory(getTestFile("target/quickstart"));
// ----------------------------------------------------------------------
// This needs to be encapsulated in a maven test case.
// ----------------------------------------------------------------------
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) getContainer().lookup(ArtifactRepositoryLayout.ROLE);
String mavenRepoLocal = getTestFile("target/local-repository").toURI().toURL().toString();
ArtifactRepository localRepository = new DefaultArtifactRepository("local", mavenRepoLocal, layout);
List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
String mavenRepoRemote = getTestFile("src/test/repository").toURI().toURL().toString();
ArtifactRepository remoteRepository = new DefaultArtifactRepository("remote", mavenRepoRemote, layout);
remoteRepositories.add(remoteRepository);
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRemoteRepositories(remoteRepositories);
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
buildingRequest.setRepositorySession(repositorySession);
ArchetypeGenerationRequest request = new ArchetypeGenerationRequest().setProjectBuildingRequest(buildingRequest).setPackage("org.apache.maven.quickstart").setGroupId("maven").setArtifactId("quickstart").setVersion("1.0-alpha-1-SNAPSHOT").setArchetypeGroupId("org.apache.maven.archetypes").setArchetypeArtifactId("maven-archetype-quickstart").setArchetypeVersion("1.0-alpha-1-SNAPSHOT").setLocalRepository(localRepository).setRemoteArtifactRepositories(remoteRepositories).setOutputDirectory(getTestFile("target").getAbsolutePath());
// parameters.put( "name", "jason" );
archetype.createArchetype(request, remoteRepository);
// ----------------------------------------------------------------------
// Set up the Velocity context
// ----------------------------------------------------------------------
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("basedir", request.getOutputDirectory());
parameters.put("package", request.getPackage());
parameters.put("packageName", request.getPackage());
parameters.put("groupId", request.getGroupId());
parameters.put("artifactId", request.getArtifactId());
parameters.put("version", request.getVersion());
Context context = new VelocityContext();
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
context.put(entry.getKey(), entry.getValue());
}
// ----------------------------------------------------------------------
// Validate POM generation
// ----------------------------------------------------------------------
ArtifactFactory artifactFactory = (ArtifactFactory) lookup(ArtifactFactory.class.getName());
Artifact archetypeArtifact = artifactFactory.createArtifact(request.getArchetypeGroupId(), request.getArchetypeArtifactId(), request.getArchetypeVersion(), Artifact.SCOPE_RUNTIME, "jar");
StringWriter writer = new StringWriter();
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getContextClassloader(archetypeArtifact, localRepository, remoteRepositories));
try {
VelocityComponent velocity = (VelocityComponent) lookup(VelocityComponent.class.getName());
velocity.getEngine().mergeTemplate(OldArchetype.ARCHETYPE_RESOURCES + "/" + OldArchetype.ARCHETYPE_POM, context, writer);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
Model generatedModel, templateModel;
try {
StringReader strReader = new StringReader(writer.toString());
MavenXpp3Reader reader = new MavenXpp3Reader();
templateModel = reader.read(strReader);
} catch (IOException e) {
throw new ArchetypeTemplateProcessingException("Error reading template POM", e);
}
File artifactDir = getTestFile("target", (String) parameters.get("artifactId"));
File pomFile = getTestFile(artifactDir.getAbsolutePath(), OldArchetype.ARCHETYPE_POM);
try {
FileReader pomReader = new FileReader(pomFile);
MavenXpp3Reader reader = new MavenXpp3Reader();
generatedModel = reader.read(pomReader);
} catch (IOException e) {
throw new ArchetypeTemplateProcessingException("Error reading generated POM", e);
} catch (XmlPullParserException e) {
throw new ArchetypeTemplateProcessingException("Error reading generated POM", e);
}
assertEquals("Generated POM ArtifactId is not equivalent to expected result.", generatedModel.getArtifactId(), templateModel.getArtifactId());
assertEquals("Generated POM GroupId is not equivalent to expected result.", generatedModel.getGroupId(), templateModel.getGroupId());
assertEquals("Generated POM Id is not equivalent to expected result.", generatedModel.getId(), templateModel.getId());
assertEquals("Generated POM Version is not equivalent to expected result.", generatedModel.getVersion(), templateModel.getVersion());
assertEquals("Generated POM Packaging is not equivalent to expected result.", generatedModel.getPackaging(), templateModel.getPackaging());
assertEquals("Generated POM Developers is not equivalent to expected result.", generatedModel.getDevelopers(), templateModel.getDevelopers());
assertEquals("Generated POM Scm is not equivalent to expected result.", generatedModel.getScm(), templateModel.getScm());
}
Aggregations