Search in sources :

Example 1 with DefaultModelWriter

use of org.apache.maven.model.io.DefaultModelWriter in project syndesis by syndesisio.

the class RepackageExtensionMojo method obtainBomDependencies.

protected Set<MavenDependency> obtainBomDependencies(String urlLocation) throws IOException, MojoExecutionException {
    Artifact artifact = downloadAndInstallArtifact(urlLocation).getArtifact();
    File tempPom = new File(outputDirectory, ".syndesis-extension-plugin-temp-pom");
    try (BufferedWriter out = Files.newBufferedWriter(tempPom.toPath(), StandardCharsets.UTF_8)) {
        Dependency bom = new Dependency();
        bom.setGroupId(artifact.getGroupId());
        bom.setArtifactId(artifact.getArtifactId());
        bom.setVersion(artifact.getVersion());
        bom.setType(artifact.getExtension());
        bom.setScope("import");
        Model bomModel = new Model();
        bomModel.setDependencyManagement(new DependencyManagement());
        bomModel.getDependencyManagement().addDependency(bom);
        bomModel.setRepositories(project.getRepositories());
        MavenProject bomProject = new MavenProject();
        bomProject.setModel(bomModel);
        bomProject.setModelVersion(project.getModelVersion());
        bomProject.setGroupId(project.getGroupId());
        bomProject.setArtifactId(project.getArtifactId() + "-temp-bom");
        bomProject.setVersion(project.getVersion());
        ModelWriter modelWriter = new DefaultModelWriter();
        modelWriter.write(out, Collections.emptyMap(), bomProject.getModel());
        MavenResolverSystem resolver = Maven.resolver();
        resolver.loadPomFromFile(tempPom).importCompileAndRuntimeDependencies();
        MavenWorkingSession session = ((MavenWorkingSessionContainer) resolver).getMavenWorkingSession();
        return session.getDependencyManagement();
    } finally {
        if (!tempPom.delete()) {
            getLog().warn("Cannot delete file " + tempPom);
        }
    }
}
Also used : DefaultModelWriter(org.apache.maven.model.io.DefaultModelWriter) MavenWorkingSession(org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession) MavenWorkingSessionContainer(org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionContainer) MavenProject(org.apache.maven.project.MavenProject) Model(org.apache.maven.model.Model) MavenDependency(org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency) Dependency(org.apache.maven.model.Dependency) File(java.io.File) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) DependencyManagement(org.apache.maven.model.DependencyManagement) DefaultModelWriter(org.apache.maven.model.io.DefaultModelWriter) ModelWriter(org.apache.maven.model.io.ModelWriter) MavenResolverSystem(org.jboss.shrinkwrap.resolver.api.maven.MavenResolverSystem) BufferedWriter(java.io.BufferedWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 Dependency (org.apache.maven.model.Dependency)1 DependencyManagement (org.apache.maven.model.DependencyManagement)1 Model (org.apache.maven.model.Model)1 DefaultModelWriter (org.apache.maven.model.io.DefaultModelWriter)1 ModelWriter (org.apache.maven.model.io.ModelWriter)1 MavenProject (org.apache.maven.project.MavenProject)1 Artifact (org.eclipse.aether.artifact.Artifact)1 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)1 MavenResolverSystem (org.jboss.shrinkwrap.resolver.api.maven.MavenResolverSystem)1 MavenWorkingSession (org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession)1 MavenDependency (org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency)1 MavenWorkingSessionContainer (org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionContainer)1