Search in sources :

Example 6 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project liferay-ide by liferay.

the class MavenProjectBuilder method initBundle.

public IStatus initBundle(IProject project, String bundleUrl, IProgressMonitor monitor) throws CoreException {
    if (bundleUrl != null) {
        File pomFile = FileUtil.getFile(project.getFile("pom.xml"));
        MavenXpp3Reader mavenReader = new MavenXpp3Reader();
        MavenXpp3Writer mavenWriter = new MavenXpp3Writer();
        try (FileReader reader = new FileReader(pomFile)) {
            Model model = mavenReader.read(reader);
            if (model != null) {
                Build build = model.getBuild();
                Plugin plugin = build.getPluginsAsMap().get("com.liferay:com.liferay.portal.tools.bundle.support");
                if (plugin != null) {
                    try (FileWriter fileWriter = new FileWriter(pomFile)) {
                        Xpp3Dom origin = (Xpp3Dom) plugin.getConfiguration();
                        Xpp3Dom newConfiguration = new Xpp3Dom("configuration");
                        Xpp3Dom url = new Xpp3Dom("url");
                        url.setValue(bundleUrl);
                        newConfiguration.addChild(url);
                        plugin.setConfiguration(Xpp3Dom.mergeXpp3Dom(newConfiguration, origin));
                        mavenWriter.write(fileWriter, model);
                    }
                }
            }
        } catch (Exception e) {
            LiferayMavenCore.logError("Could not write file in" + pomFile, e);
        }
    }
    IMavenProjectFacade facade = MavenUtil.getProjectFacade(project, monitor);
    if (_execMavenLaunch(project, MavenGoalUtil.getMavenInitBundleGoal(project), facade, monitor)) {
        return Status.OK_STATUS;
    }
    return LiferayMavenCore.createErrorStatus("run init-bundle error");
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) FileWriter(java.io.FileWriter) Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IFile(org.eclipse.core.resources.IFile) File(java.io.File) CoreException(org.eclipse.core.runtime.CoreException) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) DebugPlugin(org.eclipse.debug.core.DebugPlugin) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) Plugin(org.apache.maven.model.Plugin) MavenPlugin(org.eclipse.m2e.core.MavenPlugin)

Example 7 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project archiva by apache.

the class LegacyToDefaultConverter method copyPom.

@SuppressWarnings("unchecked")
private boolean copyPom(Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction) throws ArtifactConversionException {
    Artifact pom = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
    pom.setBaseVersion(artifact.getBaseVersion());
    ArtifactRepository repository = artifact.getRepository();
    Path file = Paths.get(repository.getBasedir(), repository.pathOf(pom));
    boolean result = true;
    if (Files.exists(file)) {
        Path targetFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(pom));
        String contents = null;
        boolean checksumsValid = false;
        try {
            if (testChecksums(artifact, file)) {
                checksumsValid = true;
            }
            // Even if the checksums for the POM are invalid we should still convert the POM
            contents = org.apache.archiva.common.utils.FileUtils.readFileToString(file, Charset.defaultCharset());
        } catch (IOException e) {
            throw new ArtifactConversionException(Messages.getString("unable.to.read.source.pom", e.getMessage()), // $NON-NLS-1$
            e);
        }
        if (// $NON-NLS-1$
        checksumsValid && contents.indexOf("modelVersion") >= 0) {
            // v4 POM
            boolean matching = false;
            if (!force && Files.exists(targetFile)) {
                String targetContents = org.apache.archiva.common.utils.FileUtils.readFileToString(targetFile, Charset.defaultCharset());
                matching = targetContents.equals(contents);
            }
            if (force || !matching) {
                transaction.createFile(contents, targetFile, digesters);
            }
        } else {
            // v3 POM
            try (StringReader stringReader = new StringReader(contents)) {
                try (StringWriter writer = new StringWriter()) {
                    org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
                    org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read(stringReader);
                    if (doRelocation(artifact, v3Model, targetRepository, transaction)) {
                        Artifact relocatedPom = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
                        targetFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(relocatedPom));
                    }
                    Model v4Model = translator.translate(v3Model);
                    translator.validateV4Basics(v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), v3Model.getPackage());
                    MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
                    xpp3Writer.write(writer, v4Model);
                    transaction.createFile(writer.toString(), targetFile, digesters);
                    List<String> warnings = translator.getWarnings();
                    for (String message : warnings) {
                        addWarning(artifact, message);
                    }
                } catch (XmlPullParserException e) {
                    addWarning(artifact, // $NON-NLS-1$
                    Messages.getString("invalid.source.pom", e.getMessage()));
                    result = false;
                } catch (IOException e) {
                    throw new ArtifactConversionException(Messages.getString("unable.to.write.converted.pom"), // $NON-NLS-1$
                    e);
                } catch (PomTranslationException e) {
                    addWarning(artifact, // $NON-NLS-1$
                    Messages.getString("invalid.source.pom", e.getMessage()));
                    result = false;
                }
            }
        }
    } else {
        // $NON-NLS-1$
        addWarning(artifact, Messages.getString("warning.missing.pom"));
    }
    return result;
}
Also used : Path(java.nio.file.Path) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) PomTranslationException(org.apache.maven.model.converter.PomTranslationException) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 8 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project fabric8-maven-plugin by fabric8io.

the class BaseBoosterIT method updateSourceCode.

protected void updateSourceCode(Repository repository, String relativePomPath) throws XmlPullParserException, IOException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    String baseDir = repository.getWorkTree().getAbsolutePath();
    Model model = reader.read(new FileInputStream(new File(baseDir, relativePomPath)));
    Dependency dependency = new Dependency();
    dependency.setGroupId("org.apache.commons");
    dependency.setArtifactId("commons-lang3");
    dependency.setVersion("3.5");
    model.getDependencies().add(dependency);
    MavenXpp3Writer writer = new MavenXpp3Writer();
    writer.write(new FileOutputStream(new File(baseDir, relativePomPath)), model);
    model.getArtifactId();
}
Also used : MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 9 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project dkpro-tc by dkpro.

the class ModelVersionIO method getCurrentTcVersionFromJar.

default String getCurrentTcVersionFromJar() throws Exception {
    Class<?> contextClass = getClass();
    InputStream stream = contextClass.getResourceAsStream("/META-INF/maven/org.dkpro.tc/dkpro-tc-core/pom.xml");
    if (stream == null) {
        return null;
    }
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    try {
        model = reader.read(stream);
    } finally {
        IOUtils.closeQuietly(stream);
    }
    String version = model.getParent().getVersion();
    return version;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader)

Example 10 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project dkpro-tc by dkpro.

the class ModelVersionIO method getCurrentTcVersionFromWorkspace.

default String getCurrentTcVersionFromWorkspace() throws Exception {
    Class<?> contextClass = getClass();
    // Try to determine the location of the POM file belonging to the
    // context object
    URL url = contextClass.getResource(contextClass.getSimpleName() + ".class");
    String classPart = contextClass.getName().replace(".", "/") + ".class";
    String base = url.toString();
    base = base.substring(0, base.length() - classPart.length());
    base = base.substring(0, base.length() - "target/classes/".length());
    File pomFile = new File(new File(URI.create(base)), "pom.xml");
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(pomFile);
        model = reader.read(fis);
    } finally {
        IOUtils.closeQuietly(fis);
    }
    String version = model.getParent().getVersion();
    return version;
}
Also used : Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Aggregations

MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)55 Model (org.apache.maven.model.Model)50 IOException (java.io.IOException)35 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)30 FileReader (java.io.FileReader)23 File (java.io.File)15 Reader (java.io.Reader)15 FileNotFoundException (java.io.FileNotFoundException)14 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 Path (java.nio.file.Path)7 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)7 MavenXpp3Writer (org.apache.maven.model.io.xpp3.MavenXpp3Writer)6 PluginException (org.bimserver.shared.exceptions.PluginException)6 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)6 InputStream (java.io.InputStream)5 StringReader (java.io.StringReader)5 SPluginBundle (org.bimserver.interfaces.objects.SPluginBundle)5 SPluginBundleVersion (org.bimserver.interfaces.objects.SPluginBundleVersion)5 FileInputStream (java.io.FileInputStream)4 DelegatingClassLoader (org.bimserver.plugins.classloaders.DelegatingClassLoader)4