use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class DescribeMojo method isReportGoal.
/**
* Determines if this Mojo should be used as a report or not. This resolves the plugin project along with all of its
* transitive dependencies to determine if the Java class of this goal implements <code>MavenReport</code>.
*
* @param md Mojo descriptor
* @return Whether or not this goal should be used as a report.
*/
private boolean isReportGoal(MojoDescriptor md) {
PluginDescriptor pd = md.getPluginDescriptor();
List<URL> urls = new ArrayList<URL>();
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setRemoteRepositories(remoteRepositories);
pbr.setResolveDependencies(true);
pbr.setProject(null);
pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
try {
Artifact jar = artifactResolver.resolveArtifact(pbr, toArtifactCoordinate(pd, "jar")).getArtifact();
Artifact pom = artifactResolver.resolveArtifact(pbr, toArtifactCoordinate(pd, "pom")).getArtifact();
MavenProject project = projectBuilder.build(pom.getFile(), pbr).getProject();
urls.add(jar.getFile().toURI().toURL());
for (Object artifact : project.getCompileClasspathElements()) {
urls.add(new File((String) artifact).toURI().toURL());
}
ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
return MavenReport.class.isAssignableFrom(Class.forName(md.getImplementation(), false, classLoader));
} catch (Exception e) {
getLog().warn("Couldn't identify if this goal is a report goal: " + e.getMessage());
return false;
}
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class AbstractHelpMojo method getMavenProject.
/**
* Retrieves the Maven Project associated with the given artifact String, in the form of
* <code>groupId:artifactId[:version]</code>. This resolves the POM artifact at those coordinates and then builds
* the Maven project from it.
*
* @param artifactString Coordinates of the Maven project to get.
* @return New Maven project.
* @throws MojoExecutionException If there was an error while getting the Maven project.
*/
protected MavenProject getMavenProject(String artifactString) throws MojoExecutionException {
ArtifactCoordinate coordinate = getArtifactCoordinate(artifactString, "pom");
try {
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setRemoteRepositories(remoteRepositories);
pbr.setProject(null);
pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
pbr.setResolveDependencies(true);
Artifact artifact = artifactResolver.resolveArtifact(pbr, coordinate).getArtifact();
return projectBuilder.build(artifact.getFile(), pbr).getProject();
} catch (Exception e) {
throw new MojoExecutionException("Unable to get the POM for the artifact '" + artifactString + "'. Verify the artifact parameter.", e);
}
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class InstallFileMojoTest method createMavenSession.
private MavenSession createMavenSession() {
MavenSession session = mock(MavenSession.class);
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManager(new File(LOCAL_REPO)));
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession(repositorySession);
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
return session;
}
use of org.apache.maven.project.DefaultProjectBuildingRequest 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.DefaultProjectBuildingRequest 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