use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-archetype by apache.
the class LocalCatalogArchetypeDataSourceTest method testLocalCatalog.
public void testLocalCatalog() throws Exception {
ArchetypeManager archetype = lookup(ArchetypeManager.class);
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(getTestFile("target/test-classes/repositories/test-catalog")));
buildingRequest.setRepositorySession(repositorySession);
ArchetypeCatalog result = archetype.getLocalCatalog(buildingRequest);
assertEquals(1, result.getArchetypes().size());
assertEquals("groupId", result.getArchetypes().get(0).getGroupId());
assertEquals("artifactId", result.getArchetypes().get(0).getArtifactId());
assertEquals("1", result.getArchetypes().get(0).getVersion());
assertEquals("http://localhost:0/repo/", result.getArchetypes().get(0).getRepository());
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-archetype by apache.
the class ArchetypeGenerationTest method testProjectGenerationFromAnArchetype.
public void testProjectGenerationFromAnArchetype() throws Exception {
ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.ROLE);
// In the embedder the localRepository will be retrieved from the embedder itself and users won't
// have to go through this muck.
ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target/test-classes/repositories/local").toURI().toURL().toExternalForm(), "local-repo");
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager("target/test-classes/repositories/central"));
buildingRequest.setRepositorySession(repositorySession);
ArchetypeCatalog catalog = archetype.getLocalCatalog(buildingRequest);
System.err.println("archetypes => " + catalog.getArchetypes());
// Here I am just grabbing a OldArchetype but in a UI you would take the OldArchetype objects and present
// them to the user.
Archetype selection = (Archetype) catalog.getArchetypes().get(catalog.getArchetypes().size() - 1);
System.err.println("Selected OldArchetype = " + selection);
// Now you will present a dialog, or whatever, and grab the following values.
String groupId = "com.mycompany";
String artifactId = "app";
String version = "1.0.0";
String packageName = "org.mycompany.app";
// With the selected OldArchetype and the parameters you can create a generation request as follows:
File outputDirectory = new File(getBasedir(), "target/test-classes/projects/archetyper-generate-1");
FileUtils.forceDelete(outputDirectory);
ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest(selection).setOutputDirectory(outputDirectory.getAbsolutePath()).setLocalRepository(localRepository).setGroupId(groupId).setArtifactId(artifactId).setVersion(version).setPackage(packageName);
Properties archetypeRequiredProperties = new Properties();
archetypeRequiredProperties.setProperty("property-with-default-1", "value-1");
archetypeRequiredProperties.setProperty("property-with-default-2", "value-2");
archetypeRequiredProperties.setProperty("property-with-default-3", "value-3");
archetypeRequiredProperties.setProperty("property-with-default-4", "value-4");
archetypeRequiredProperties.setProperty("property-without-default-1", "some-value-1");
archetypeRequiredProperties.setProperty("property-without-default-2", "some-value-2");
archetypeRequiredProperties.setProperty("property-without-default-3", "some-value-3");
archetypeRequiredProperties.setProperty("property-without-default-4", "some-value-4");
archetypeRequiredProperties.setProperty("property_underscored_1", "prop1");
archetypeRequiredProperties.setProperty("property_underscored-2", "prop2");
agr.setProperties(archetypeRequiredProperties);
agr.setProjectBuildingRequest(buildingRequest);
// Then generate away!
ArchetypeGenerationResult result = archetype.generateProjectFromArchetype(agr);
if (result.getCause() != null) {
result.getCause().printStackTrace(System.err);
fail(result.getCause().getMessage());
}
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-archetype by apache.
the class InternalCatalogArchetypesVerificationTest method testInternalCatalog.
public void testInternalCatalog() throws Exception {
ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target/test-classes/repositories/local").toURI().toURL().toExternalForm(), "local-repo");
File outputDirectory = new File(getBasedir(), "target/internal-archetypes-projects");
outputDirectory.mkdirs();
FileUtils.cleanDirectory(outputDirectory);
ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.class.getName());
ArchetypeCatalog catalog = archetype.getInternalCatalog();
int count = 1;
for (Archetype a : catalog.getArchetypes()) {
Archetype ar = new Archetype();
ar.setGroupId(a.getGroupId());
ar.setArtifactId(a.getArtifactId());
ar.setVersion(a.getVersion());
ar.setDescription(a.getDescription());
ar.setGoals(a.getGoals());
ar.setProperties(a.getProperties());
ar.setRepository(a.getRepository());
if (ar.getRepository() == null) {
ar.setRepository(CENTRAL);
}
ArchetypeGenerationRequest request = new ArchetypeGenerationRequest(ar).setGroupId("org.apache.maven.archetype.test").setArtifactId("archetype" + count).setVersion("1.0-SNAPSHOT").setPackage("com.acme").setOutputDirectory(outputDirectory.getPath()).setLocalRepository(localRepository);
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
buildingRequest.setRepositorySession(repositorySession);
request.setProjectBuildingRequest(buildingRequest);
ArchetypeGenerationResult generationResult = archetype.generateProjectFromArchetype(request);
assertTrue("Archetype wasn't generated successfully", generationResult.getCause() == null);
count++;
}
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class AbstractChangesReport method getSkinArtifactFile.
private File getSkinArtifactFile() throws MojoExecutionException {
Skin skin = Skin.getDefaultSkin();
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(skin.getGroupId());
coordinate.setArtifactId(skin.getArtifactId());
coordinate.setVersion(skin.getVersion());
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(mavenSession.getProjectBuildingRequest());
pbr.setRemoteRepositories(project.getRemoteArtifactRepositories());
try {
return resolver.resolveArtifact(pbr, coordinate).getArtifact().getFile();
} catch (ArtifactResolverException e) {
throw new MojoExecutionException("Couldn't resolve the skin.", e);
}
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class InstallFileMojo method createMavenProject.
/**
* Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is
* supplied, the packaging must be POM because the project with only have attachments. This project serves as basis
* to attach the artifacts to install to.
*
* @return The created Maven project, never <code>null</code>.
* @throws MojoExecutionException When the model of the project could not be built.
* @throws MojoFailureException When building the project failed.
*/
private MavenProject createMavenProject() throws MojoExecutionException, MojoFailureException {
if (groupId == null || artifactId == null || version == null || packaging == null) {
throw new MojoExecutionException("The artifact information is incomplete: 'groupId', 'artifactId', " + "'version' and 'packaging' are required.");
}
ModelSource modelSource = new StringModelSource("<project><modelVersion>4.0.0</modelVersion><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>" + version + "</version><packaging>" + (classifier == null ? packaging : "pom") + "</packaging></project>");
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setProcessPlugins(false);
try {
return projectBuilder.build(modelSource, pbr).getProject();
} catch (ProjectBuildingException e) {
if (e.getCause() instanceof ModelBuildingException) {
throw new MojoExecutionException("The artifact information is not valid:" + Os.LINE_SEP + e.getCause().getMessage());
}
throw new MojoFailureException("Unable to create the project.", e);
}
}
Aggregations