use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project maven-archetype by apache.
the class DefaultRepositoryCrawler method writeCatalog.
public boolean writeCatalog(ArchetypeCatalog archetypeCatalog, File archetypeCatalogFile) {
Writer fileWriter = null;
try {
ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
fileWriter = WriterFactory.newXmlWriter(archetypeCatalogFile);
catalogWriter.write(fileWriter, archetypeCatalog);
return true;
} catch (IOException ex) {
getLogger().warn("Catalog can not be writen to " + archetypeCatalogFile, ex);
return false;
} finally {
IOUtil.close(fileWriter);
}
}
use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project maven-archetype by apache.
the class ArchetyperRoundtripIT method testArchetyper.
public void testArchetyper() throws Exception {
ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.ROLE);
ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "local").toURI().toURL().toExternalForm(), "local-repo");
ArtifactRepository centralRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "central").toURI().toURL().toExternalForm(), "central-repo");
// (1) create a project from scratch
// (2) create an archetype from the project
// (3) create our own archetype catalog properties in memory
// (4) create our own archetype catalog describing the archetype we just created
// (5) deploy the archetype we just created
// (6) create a project form the archetype we just created
// ------------------------------------------------------------------------
//
// ------------------------------------------------------------------------
// (1) create a project from scratch
// File sourceProject = new File( getBasedir( ), "target/test-classes/projects/roundtrip-1-project" );
File workingProject = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-1-project");
FileUtils.forceDelete(new File(workingProject, "target"));
// (2) create an archetype from the project
File pom = new File(workingProject, "pom.xml");
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
buildingRequest.setRepositorySession(repositorySession);
MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
Properties properties = new Properties();
properties.setProperty("someProperty", "someValue");
ArchetypeCreationRequest acr = new ArchetypeCreationRequest().setProject(project).setLocalRepository(localRepository).setProperties(properties).setPostPhase("package");
ArchetypeCreationResult creationResult = archetype.createArchetypeFromProject(acr);
if (creationResult.getCause() != null) {
throw creationResult.getCause();
}
// (3) create our own archetype catalog properties in memory
File catalogDirectory = new File(getBasedir(), "target" + File.separator + "catalog");
catalogDirectory.mkdirs();
File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
// (5) install the archetype we just created
File generatedArchetypeDirectory = new File(project.getBasedir(), "target" + File.separator + "generated-sources" + File.separator + "archetype");
File generatedArchetypePom = new File(generatedArchetypeDirectory, "pom.xml");
ProjectBuildingResult buildingResult = projectBuilder.build(generatedArchetypePom, buildingRequest);
MavenProject generatedArchetypeProject = projectBuilder.build(generatedArchetypePom, buildingRequest).getProject();
ModelInterpolator modelInterpolator = (ModelInterpolator) lookup(ModelInterpolator.ROLE);
Model generatedArchetypeModel = modelInterpolator.interpolate(generatedArchetypeProject.getModel(), generatedArchetypePom.getParentFile(), new DefaultProjectBuilderConfiguration(), true);
File archetypeDirectory = new File(generatedArchetypeDirectory, "src" + File.separator + "main" + File.separator + "resources");
File archetypeArchive = archetype.archiveArchetype(archetypeDirectory, new File(generatedArchetypeModel.getBuild().getDirectory()), generatedArchetypeModel.getBuild().getFinalName());
String baseName = StringUtils.replace(generatedArchetypeProject.getGroupId(), ".", File.separator) + File.separator + generatedArchetypeProject.getArtifactId() + File.separator + generatedArchetypeProject.getVersion() + File.separator + generatedArchetypeProject.getBuild().getFinalName();
File archetypeInRepository = new File(centralRepository.getBasedir(), baseName + ".jar");
File archetypePomInRepository = new File(centralRepository.getBasedir(), baseName + ".pom");
archetypeInRepository.getParentFile().mkdirs();
FileUtils.copyFile(archetypeArchive, archetypeInRepository);
FileUtils.copyFile(generatedArchetypePom, archetypePomInRepository);
// (4) create our own archetype catalog describing the archetype we just created
ArchetypeCatalog catalog = new ArchetypeCatalog();
Archetype generatedArchetype = new Archetype();
generatedArchetype.setGroupId(generatedArchetypeProject.getGroupId());
generatedArchetype.setArtifactId(generatedArchetypeProject.getArtifactId());
generatedArchetype.setVersion(generatedArchetypeProject.getVersion());
generatedArchetype.setRepository("http://localhost:" + port + "/repo");
catalog.addArchetype(generatedArchetype);
ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
Writer writer = new FileWriter(catalogFile);
catalogWriter.write(writer, catalog);
IOUtils.closeQuietly(writer);
// (6) create a project form the archetype we just created
String outputDirectory = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-1-recreatedproject").getAbsolutePath();
FileUtils.forceDelete(outputDirectory);
ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest().setArchetypeGroupId(generatedArchetypeProject.getGroupId()).setArchetypeArtifactId(generatedArchetypeProject.getArtifactId()).setArchetypeVersion(generatedArchetypeProject.getVersion()).setGroupId("com.mycompany").setArtifactId("myapp").setVersion("1.0-SNAPSHOT").setPackage("com.mycompany.myapp").setProperties(properties).setOutputDirectory(outputDirectory).setLocalRepository(localRepository).setArchetypeRepository("http://localhost:" + port + "/repo/").setProjectBuildingRequest(buildingRequest);
ArchetypeGenerationResult generationResult = archetype.generateProjectFromArchetype(agr);
if (generationResult.getCause() != null) {
throw generationResult.getCause();
}
// ASSERT symbol_pound replacement (archetype-180 archetype-183)
String content = FileUtils.fileRead(outputDirectory + File.separator + "myapp" + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + "com" + File.separator + "mycompany" + File.separator + "myapp" + File.separator + "App.java");
System.err.println("content=" + content);
assertTrue(content.indexOf("//A #\\{some}") > 0);
assertTrue(content.indexOf("//B #{some}") > 0);
assertTrue(content.indexOf("//C #{some other}") > 0);
assertTrue(content.indexOf("//D \\#{some other}") > 0);
assertTrue(content.indexOf("//E #{}") > 0);
assertTrue(content.indexOf("//F {some}") > 0);
assertTrue(content.indexOf("//G ${someOtherProperty}") > 0);
assertTrue(content.indexOf("//H ${someValue}") > 0);
assertTrue(content.indexOf("/*") > 0);
assertTrue(content.indexOf(" A #\\{some}") > 0);
assertTrue(content.indexOf(" B #{some}") > 0);
assertTrue(content.indexOf(" C #{some other}") > 0);
assertTrue(content.indexOf(" D \\#{some other}") > 0);
assertTrue(content.indexOf(" E #{}") > 0);
assertTrue(content.indexOf(" F {some}") > 0);
assertTrue(content.indexOf(" G ${someOtherProperty}") > 0);
assertTrue(content.indexOf(" H ${someValue}") > 0);
// Assert symbol_dollar archetype-138
}
use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project maven-archetype by apache.
the class RoundtripMultiModuleIT method testArchetyper.
public void testArchetyper() throws Exception {
ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.ROLE);
ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "local").toURI().toURL().toExternalForm(), "local-repo");
ArtifactRepository centralRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "central").toURI().toURL().toExternalForm(), "central-repo");
// (1) create a project from scratch
// (2) create an archetype from the project
// (3) create our own archetype catalog properties in memory
// (4) create our own archetype catalog describing the archetype we just created
// (5) deploy the archetype we just created
// (6) create a project form the archetype we just created
// ------------------------------------------------------------------------
//
// ------------------------------------------------------------------------
// (1) create a project from scratch
// File sourceProject = new File( getBasedir( ), "target/test-classes/projects/roundtrip-1-project" );
File workingProject = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-multi");
FileUtils.forceDelete(new File(workingProject, "target"));
// (2) create an archetype from the project
File pom = new File(workingProject, "pom.xml");
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
buildingRequest.setRepositorySession(repositorySession);
MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
ModelInterpolator modelInterpolator = (ModelInterpolator) lookup(ModelInterpolator.ROLE);
ArchetypeCreationRequest acr = new ArchetypeCreationRequest().setProject(project).setLocalRepository(localRepository).setFiltereds(Constants.DEFAULT_FILTERED_EXTENSIONS).setLanguages(Constants.DEFAULT_LANGUAGES).setPostPhase("package");
ArchetypeCreationResult creationResult = archetype.createArchetypeFromProject(acr);
if (creationResult.getCause() != null) {
throw creationResult.getCause();
} else {
assertArchetypeCreated(workingProject);
}
// (3) create our own archetype catalog properties in memory
File catalogDirectory = new File(getBasedir(), "target" + File.separator + "catalog");
File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
File catalogProperties = new File(catalogDirectory, "archetype-catalog.properties");
catalogDirectory.mkdirs();
Properties p = new Properties();
p.setProperty("sources", "catalog");
p.setProperty("catalog.file", catalogFile.getAbsolutePath());
OutputStream os = new FileOutputStream(catalogProperties);
p.store(os, "Generated catalog properties");
// (5) install the archetype we just created
File generatedArchetypeDirectory = new File(project.getBasedir(), "target" + File.separator + "generated-sources" + File.separator + "archetype");
File generatedArchetypePom = new File(generatedArchetypeDirectory, "pom.xml");
MavenProject generatedArchetypeProject = projectBuilder.build(generatedArchetypePom, buildingRequest).getProject();
Model generatedModel = modelInterpolator.interpolate(generatedArchetypeProject.getModel(), generatedArchetypePom.getParentFile(), new DefaultProjectBuilderConfiguration(), true);
File archetypeDirectory = new File(generatedArchetypeDirectory, "src" + File.separator + "main" + File.separator + "resources");
File archetypeArchive = archetype.archiveArchetype(archetypeDirectory, new File(generatedModel.getBuild().getDirectory()), generatedModel.getBuild().getFinalName());
String baseName = StringUtils.replace(generatedArchetypeProject.getGroupId(), ".", File.separator) + File.separator + generatedArchetypeProject.getArtifactId() + File.separator + generatedArchetypeProject.getVersion() + File.separator + generatedModel.getBuild().getFinalName();
File archetypeInRepository = new File(centralRepository.getBasedir(), baseName + ".jar");
File archetypePomInRepository = new File(centralRepository.getBasedir(), baseName + ".pom");
archetypeInRepository.getParentFile().mkdirs();
FileUtils.copyFile(archetypeArchive, archetypeInRepository);
FileUtils.copyFile(generatedArchetypePom, archetypePomInRepository);
// (4) create our own archetype catalog describing the archetype we just created
ArchetypeCatalog catalog = new ArchetypeCatalog();
Archetype generatedArchetype = new Archetype();
generatedArchetype.setGroupId(generatedArchetypeProject.getGroupId());
generatedArchetype.setArtifactId(generatedArchetypeProject.getArtifactId());
generatedArchetype.setVersion(generatedArchetypeProject.getVersion());
generatedArchetype.setRepository("http://localhost:" + port + "/repo");
catalog.addArchetype(generatedArchetype);
ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
Writer writer = new FileWriter(catalogFile);
catalogWriter.write(writer, catalog);
IOUtils.closeQuietly(writer);
// (6) create a project form the archetype we just created
String outputDirectory = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-multi-recreated").getAbsolutePath();
FileUtils.forceDelete(outputDirectory);
ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest().setArchetypeGroupId(generatedArchetypeProject.getGroupId()).setArchetypeArtifactId(generatedArchetypeProject.getArtifactId()).setArchetypeVersion(generatedArchetypeProject.getVersion()).setGroupId("com.mycompany").setArtifactId("myapp").setVersion("1.0-SNAPSHOT").setPackage("com.mycompany.myapp").setOutputDirectory(outputDirectory).setLocalRepository(localRepository).setArchetypeRepository("http://localhost:" + port + "/repo/").setProjectBuildingRequest(buildingRequest);
ArchetypeGenerationResult generationResult = archetype.generateProjectFromArchetype(agr);
if (generationResult.getCause() != null) {
fail(generationResult.getCause().getMessage());
}
String myapp = outputDirectory + File.separator + "myapp" + File.separator;
assertTrue(new File(myapp + "myapp-api", ".classpath").exists());
assertTrue(new File(myapp + "myapp-cli", ".classpath").exists());
assertTrue(new File(myapp + "myapp-core", ".classpath").exists());
assertTrue(new File(myapp + "myapp-model", ".classpath").exists());
assertTrue(new File(myapp + File.separator + "myapp-stores" + File.separator + "myapp-store-memory", ".classpath").exists());
assertTrue(new File(myapp + "myapp-stores" + File.separator + "myapp-store-xstream", ".classpath").exists());
assertTrue(new File(myapp + "myapp-api", ".checkstyle").exists());
assertTrue(new File(myapp + "myapp-cli", ".checkstyle").exists());
assertTrue(new File(myapp + "myapp-core", ".checkstyle").exists());
assertTrue(new File(myapp + "myapp-model", ".checkstyle").exists());
assertTrue(new File(myapp + "myapp-stores" + File.separator + "myapp-store-memory", ".checkstyle").exists());
assertTrue(new File(myapp + "myapp-stores" + File.separator + "myapp-store-xstream", ".checkstyle").exists());
}
use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project maven-archetype by apache.
the class LocalCatalogArchetypeDataSourceTest method setUp.
protected void setUp() throws Exception {
super.setUp();
File catalogDirectory = getTestFile("target/test-classes/repositories/test-catalog");
catalogDirectory.mkdirs();
ArchetypeCatalog catalog = new ArchetypeCatalog();
Archetype generatedArchetype = new Archetype();
generatedArchetype.setGroupId("groupId");
generatedArchetype.setArtifactId("artifactId");
generatedArchetype.setVersion("1");
generatedArchetype.setRepository("http://localhost:0/repo/");
catalog.addArchetype(generatedArchetype);
File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
Writer writer = new FileWriter(catalogFile);
catalogWriter.write(writer, catalog);
IOUtils.closeQuietly(writer);
}
use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project maven-archetype by apache.
the class RemoteCatalogArchetypeDataSourceTest method setUp.
protected void setUp() throws Exception {
super.setUp();
System.setProperty("org.apache.maven.archetype.repository.directory", getTestPath("target/test-classes/repositories/test-catalog"));
// Start Jetty
server = new Server(0);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/repo");
webapp.setWar("target/wars/archetype-repository.war");
server.setHandler(webapp);
server.start();
port = server.getConnectors()[0].getLocalPort();
File catalogDirectory = getTestFile("target/test-classes/repositories/test-catalog");
catalogDirectory.mkdirs();
getTestFile("target/test-classes/repositories/test-catalog/dummy").createNewFile();
ArchetypeCatalog catalog = new ArchetypeCatalog();
Archetype generatedArchetype = new Archetype();
generatedArchetype.setGroupId("groupId");
generatedArchetype.setArtifactId("artifactId");
generatedArchetype.setVersion("1");
generatedArchetype.setRepository("http://localhost:" + port + "/repo/");
catalog.addArchetype(generatedArchetype);
File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
Writer writer = new FileWriter(catalogFile);
catalogWriter.write(writer, catalog);
IOUtils.closeQuietly(writer);
}
Aggregations