Search in sources :

Example 1 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project tycho by eclipse.

the class TargetPlatformFilterConfigurationReaderTest method buildProjectModel.

// TODO share this?
MavenProject buildProjectModel(File pom) throws Exception, ProjectBuildingException {
    ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
    List<ProjectBuildingResult> projects = projectBuilder.build(Collections.singletonList(pom), false, projectBuildRequestForUnitTest());
    MavenProject project = projects.get(0).getProject();
    return project;
}
Also used : ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult)

Example 2 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project component-runtime by Talend.

the class ArtifactTransformer method hasTransformedResource.

@Override
public boolean hasTransformedResource() {
    artifacts = new ArrayList<>();
    final ArtifactFilter filter = getFilter();
    if (userArtifacts != null && !userArtifacts.isEmpty()) {
        final ArtifactResolver resolver;
        final DependencyGraphBuilder graphBuilder;
        final ProjectBuilder projectBuilder;
        final PlexusContainer container = session.getContainer();
        try {
            resolver = ArtifactResolver.class.cast(container.lookup(ArtifactResolver.class, "default"));
            projectBuilder = ProjectBuilder.class.cast(container.lookup(ProjectBuilder.class, "default"));
            graphBuilder = includeTransitiveDependencies ? DependencyGraphBuilder.class.cast(container.lookup(DependencyGraphBuilder.class, "default")) : null;
        } catch (final ComponentLookupException e) {
            throw new IllegalArgumentException(e);
        }
        artifacts.addAll(userArtifacts.stream().flatMap(coords -> {
            try {
                final String type = ofNullable(coords.type).filter(s -> !s.isEmpty()).orElse("jar");
                final Artifact art = new DefaultArtifact(coords.groupId, coords.artifactId, coords.version, coords.scope, type, ofNullable(coords.classifier).orElse(""), new DefaultArtifactHandler() {

                    {
                        setExtension(type);
                    }
                });
                final Artifact artifact = resolver.resolveArtifact(session.getProjectBuildingRequest(), art).getArtifact();
                if (includeTransitiveDependencies) {
                    final DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
                    try {
                        final ProjectBuildingResult projectBuildingResult = projectBuilder.build(artifact, true, request);
                        final DependencyNode transitives = graphBuilder.buildDependencyGraph(projectBuildingResult.getProject(), filter);
                        final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
                        transitives.accept(new FilteringDependencyNodeVisitor(visitor, new ArtifactDependencyNodeFilter(filter)));
                        return Stream.concat(Stream.of(artifact), visitor.getNodes().stream().map(DependencyNode::getArtifact).map(a -> {
                            try {
                                return resolver.resolveArtifact(request, a).getArtifact();
                            } catch (final ArtifactResolverException e) {
                                throw new IllegalStateException(e);
                            }
                        }));
                    } catch (final ProjectBuildingException | DependencyGraphBuilderException e) {
                        throw new IllegalStateException(e);
                    }
                }
                return Stream.of(artifact);
            } catch (final ArtifactResolverException e) {
                throw new IllegalArgumentException(e);
            }
        }).collect(toSet()));
    }
    if (includeProjectComponentDependencies) {
        // this is a weird category of projects but can exist
        final MavenProject project = session.getCurrentProject();
        if (project.getArtifacts() != null && !project.getArtifacts().isEmpty()) {
            project.setArtifactFilter(filter);
            try {
                artifacts.addAll(project.getArtifacts());
            } finally {
                // shade plugin uses it OOTB so reset it for the end of the execution (in case
                // another transformer needs it)
                project.setArtifactFilter(new CumulativeScopeArtifactFilter(singletonList("runtime")));
            }
        }
    }
    return !artifacts.isEmpty();
}
Also used : CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) Setter(lombok.Setter) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) Collectors.toSet(java.util.stream.Collectors.toSet) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) MavenSession(org.apache.maven.execution.MavenSession) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) Optional.ofNullable(java.util.Optional.ofNullable) NONE(lombok.AccessLevel.NONE) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) IOException(java.io.IOException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) PlexusContainer(org.codehaus.plexus.PlexusContainer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Stream(java.util.stream.Stream) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) Data(lombok.Data) ProjectBuilder(org.apache.maven.project.ProjectBuilder) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) InputStream(java.io.InputStream) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) PlexusContainer(org.codehaus.plexus.PlexusContainer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 3 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project maven-archetype by apache.

the class DefaultArchetypeCreatorIT method createFilesetArchetype.

protected void createFilesetArchetype(String project) throws Exception {
    System.out.println(">>>>>> testCreateFilesetArchetype( \"" + project + "\" )");
    ProjectBuilder builder = lookup(ProjectBuilder.class);
    File projectFile = getProjectFile(project);
    File projectFileSample = getProjectSampleFile(project);
    copy(projectFileSample, projectFile);
    FileUtils.deleteDirectory(new File(projectFile.getParentFile(), "target"));
    File propertyFile = getPropertiesFile(project);
    File propertyFileSample = getPropertiesSampleFile(project);
    copy(propertyFileSample, propertyFile);
    Properties p = PropertyUtils.loadProperties(propertyFile);
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    buildingRequest.setLocalRepository(localRepository);
    buildingRequest.setSystemProperties(System.getProperties());
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
    buildingRequest.setRepositorySession(repositorySession);
    MavenProject mavenProject = builder.build(projectFile, buildingRequest).getProject();
    FilesetArchetypeCreator instance = (FilesetArchetypeCreator) lookup(ArchetypeCreator.class.getName(), "fileset");
    languages = new ArrayList<String>();
    languages.add("java");
    languages.add("aspectj");
    languages.add("csharp");
    languages.add("groovy");
    languages.add("resources");
    filtereds = new ArrayList<String>();
    filtereds.add("java");
    filtereds.add("xml");
    filtereds.add("txt");
    filtereds.add("groovy");
    filtereds.add("cs");
    filtereds.add("mdo");
    filtereds.add("aj");
    filtereds.add("jsp");
    filtereds.add("js");
    filtereds.add("gsp");
    filtereds.add("vm");
    filtereds.add("html");
    filtereds.add("xhtml");
    filtereds.add("properties");
    filtereds.add(".classpath");
    filtereds.add(".project");
    filtereds.add("MF");
    ArchetypeCreationRequest request = new ArchetypeCreationRequest().setProject(mavenProject).setPackageName(p.getProperty(Constants.PACKAGE)).setProperties(p).setLanguages(languages).setFiltereds(filtereds).setDefaultEncoding("UTF-8").setPartialArchetype(false).setPreserveCData(false).setKeepParent(false).setPostPhase("verify").setLocalRepository(localRepository).setProjectBuildingRequest(buildingRequest);
    ArchetypeCreationResult result = new ArchetypeCreationResult();
    instance.createArchetype(request, result);
    if (result.getCause() != null) {
        throw result.getCause();
    }
    System.out.println("<<<<<< testCreateFilesetArchetype( \"" + project + "\" )");
}
Also used : MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) Properties(java.util.Properties) ArchetypeCreationResult(org.apache.maven.archetype.ArchetypeCreationResult) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenProjectBuilder(org.apache.maven.project.MavenProjectBuilder) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ArchetypeCreationRequest(org.apache.maven.archetype.ArchetypeCreationRequest) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 4 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder 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
}
Also used : ModelInterpolator(org.apache.maven.project.interpolation.ModelInterpolator) Archetype(org.apache.maven.archetype.catalog.Archetype) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) FileWriter(java.io.FileWriter) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) Properties(java.util.Properties) ArchetypeCreationResult(org.apache.maven.archetype.ArchetypeCreationResult) ArchetypeManager(org.apache.maven.archetype.ArchetypeManager) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) Model(org.apache.maven.model.Model) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult) ArchetypeCreationRequest(org.apache.maven.archetype.ArchetypeCreationRequest) 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)

Example 5 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder 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());
}
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

ProjectBuilder (org.apache.maven.project.ProjectBuilder)15 MavenProject (org.apache.maven.project.MavenProject)11 ProjectBuildingResult (org.apache.maven.project.ProjectBuildingResult)8 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)6 File (java.io.File)5 Properties (java.util.Properties)5 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)5 IOException (java.io.IOException)4 ArchetypeCreationRequest (org.apache.maven.archetype.ArchetypeCreationRequest)4 ArchetypeCreationResult (org.apache.maven.archetype.ArchetypeCreationResult)4 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)4 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)4 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)4 FileWriter (java.io.FileWriter)3 Writer (java.io.Writer)3 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)3 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)3 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)3 Archetype (org.apache.maven.archetype.catalog.Archetype)3 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)3