Search in sources :

Example 6 with ArchetypeCatalogXpp3Writer

use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project maven-archetype by apache.

the class CatalogArchetypeDataSource method writeLocalCatalog.

protected void writeLocalCatalog(ArchetypeCatalog catalog, File catalogFile) throws ArchetypeDataSourceException {
    Writer writer = null;
    try {
        writer = WriterFactory.newXmlWriter(catalogFile);
        ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
        catalogWriter.write(writer, catalog);
    } catch (IOException e) {
        throw new ArchetypeDataSourceException("Error writing archetype catalog.", e);
    } finally {
        IOUtil.close(writer);
    }
}
Also used : ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) IOException(java.io.IOException) Writer(java.io.Writer) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)

Example 7 with ArchetypeCatalogXpp3Writer

use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project indy by Commonjava.

the class ArchetypeCatalogMerger method merge.

public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
    final ArchetypeCatalog master = new ArchetypeCatalog();
    final ArchetypeCatalogXpp3Reader reader = new ArchetypeCatalogXpp3Reader();
    final FileReader fr = null;
    boolean merged = false;
    final Set<String> seen = new HashSet<String>();
    for (final Transfer src : sources) {
        try (InputStream stream = src.openInputStream()) {
            final ArchetypeCatalog catalog = reader.read(stream, false);
            for (final Archetype arch : catalog.getArchetypes()) {
                final String key = arch.getGroupId() + ":" + arch.getArtifactId() + ":" + arch.getVersion();
                if (seen.add(key)) {
                    master.addArchetype(arch);
                }
            }
            merged = true;
        } catch (final IOException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot read archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } catch (final XmlPullParserException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot parse archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } finally {
            closeQuietly(fr);
        }
    }
    if (merged) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            new ArchetypeCatalogXpp3Writer().write(baos, master);
            return baos.toByteArray();
        } catch (final IOException e) {
            logger.error(String.format("Cannot write consolidated archetype catalog: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
        }
    }
    return null;
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) InputStream(java.io.InputStream) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StoreKey(org.commonjava.indy.model.core.StoreKey) ArchetypeCatalogXpp3Reader(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader) Transfer(org.commonjava.maven.galley.model.Transfer) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) HashSet(java.util.HashSet)

Example 8 with ArchetypeCatalogXpp3Writer

use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer in project maven-archetype by apache.

the class ArchetyperRoundtripWithProxyIT 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-2-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();
    ArchetypeCreationRequest acr = new ArchetypeCreationRequest().setProject(project).setLocalRepository(localRepository).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");
    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();
    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 + generatedArchetypeModel.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-2-recreatedproject").getAbsolutePath();
    FileUtils.forceDelete(outputDirectory);
    // WagonManager manager = (WagonManager) lookup( WagonManager.class.getName() );
    // manager.addProxy( "http", "localhost", proxyPort, null, null, null );
    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) {
        throw generationResult.getCause();
    }
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) FileWriter(java.io.FileWriter) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Properties(java.util.Properties) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenProject(org.apache.maven.project.MavenProject) ArchetypeCreationRequest(org.apache.maven.archetype.ArchetypeCreationRequest) ModelInterpolator(org.apache.maven.project.interpolation.ModelInterpolator) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) ArchetypeCreationResult(org.apache.maven.archetype.ArchetypeCreationResult) ArchetypeManager(org.apache.maven.archetype.ArchetypeManager) ProjectBuilder(org.apache.maven.project.ProjectBuilder) FileOutputStream(java.io.FileOutputStream) Model(org.apache.maven.model.Model) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) FileWriter(java.io.FileWriter) Writer(java.io.Writer) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)

Aggregations

ArchetypeCatalogXpp3Writer (org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)8 Writer (java.io.Writer)7 Archetype (org.apache.maven.archetype.catalog.Archetype)6 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)6 File (java.io.File)5 FileWriter (java.io.FileWriter)5 IOException (java.io.IOException)3 Properties (java.util.Properties)3 ArchetypeCreationRequest (org.apache.maven.archetype.ArchetypeCreationRequest)3 ArchetypeCreationResult (org.apache.maven.archetype.ArchetypeCreationResult)3 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)3 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)3 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)3 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)3 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)3 Model (org.apache.maven.model.Model)3 DefaultProjectBuilderConfiguration (org.apache.maven.project.DefaultProjectBuilderConfiguration)3 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)3 MavenProject (org.apache.maven.project.MavenProject)3 ProjectBuilder (org.apache.maven.project.ProjectBuilder)3