Search in sources :

Example 46 with SimpleLocalRepositoryManager

use of org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager in project maven-plugins by apache.

the class AggregatorJavadocReportTest method lookupMojo.

private JavadocReport lookupMojo(File testPom) throws Exception {
    JavadocReport mojo = (JavadocReport) lookupMojo("aggregate", testPom);
    MojoExecution mojoExec = new MojoExecution(new Plugin(), "aggregate", null);
    setVariableValueToObject(mojo, "mojo", mojoExec);
    MavenProject currentProject = new MavenProjectStub();
    currentProject.setGroupId("GROUPID");
    currentProject.setArtifactId("ARTIFACTID");
    MavenSession session = newMavenSession(currentProject);
    DefaultRepositorySystemSession repoSysSession = (DefaultRepositorySystemSession) session.getRepositorySession();
    repoSysSession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    setVariableValueToObject(mojo, "session", session);
    return mojo;
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenProject(org.apache.maven.project.MavenProject) MavenProjectStub(org.apache.maven.plugin.testing.stubs.MavenProjectStub) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) MojoExecution(org.apache.maven.plugin.MojoExecution) Plugin(org.apache.maven.model.Plugin)

Example 47 with SimpleLocalRepositoryManager

use of org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager 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;
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 48 with SimpleLocalRepositoryManager

use of org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager 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());
}
Also used : HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) ArrayList(java.util.ArrayList) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) URLClassLoader(java.net.URLClassLoader) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArtifactRepositoryLayout(org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout) Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) Model(org.apache.maven.model.Model) VelocityComponent(org.codehaus.plexus.velocity.VelocityComponent) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 49 with SimpleLocalRepositoryManager

use of org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager 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

SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)49 File (java.io.File)44 MavenSession (org.apache.maven.execution.MavenSession)30 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)29 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)28 DefaultRepositorySystemSession (org.sonatype.aether.util.DefaultRepositorySystemSession)20 LegacySupport (org.apache.maven.plugin.LegacySupport)16 MavenProject (org.apache.maven.project.MavenProject)16 Artifact (org.apache.maven.artifact.Artifact)11 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)9 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)7 ArrayList (java.util.ArrayList)6 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)6 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)6 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)6 HashMap (java.util.HashMap)5 Properties (java.util.Properties)5 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)5 Archetype (org.apache.maven.archetype.catalog.Archetype)5 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)5