Example usage for org.apache.maven.project MavenProject writeModel

List of usage examples for org.apache.maven.project MavenProject writeModel

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject writeModel.

Prototype

@Deprecated
public void writeModel(Writer writer) throws IOException 

Source Link

Usage

From source file:info.debatty.sparkpackages.maven.plugin.ZipMojo.java

License:Open Source License

/**
 *
 * @throws MojoFailureException if we could not create the ZIP file
 *//*www .  j a v a  2  s . co m*/
@Override
public final void realexe() throws MojoFailureException {

    FileOutputStream dest = null;
    try {
        dest = new FileOutputStream(getZipPath());
    } catch (FileNotFoundException ex) {
        throw new MojoFailureException("Could not open destination zip file", ex);
    }

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
    byte[] data = new byte[BUFFER];

    FileInputStream fi;
    try {
        fi = new FileInputStream(new File(getJarPath()));
    } catch (FileNotFoundException ex) {
        throw new MojoFailureException("Could not read JAR", ex);
    }
    BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);

    ZipEntry jar_entry = new ZipEntry(getRepo() + "-" + getVersion() + ".jar");
    try {
        out.putNextEntry(jar_entry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            out.write(data, 0, count);
        }
        origin.close();
    } catch (IOException ex) {
        throw new MojoFailureException("Could not add JAR to ZIP", ex);
    }

    MavenProject modified_project;
    modified_project = (MavenProject) getProject().clone();

    modified_project.setArtifactId(getRepo());
    modified_project.setGroupId(getOrganization());

    ZipEntry pom_entry = new ZipEntry(getRepo() + "-" + getVersion() + ".pom");
    try {
        out.putNextEntry(pom_entry);
        modified_project.writeModel(new OutputStreamWriter(out));
        out.close();
        dest.close();
    } catch (IOException ex) {
        throw new MojoFailureException("Could add POM to ZIP", ex);
    }
}

From source file:org.andromda.maven.plugin.bootstrap.install.BootstrapInstallMojo.java

/**
 * Clears the POM's model of its parent or any dependencies
 * it may have so that we can write a POM that isn't dependent on anything
 * (which we need for bootstrap artifacts).
 *
 * @param bootstrapPomFile the bootstrap POM file to write.
 *//*from  www .ja v a 2  s  . c o m*/
private void writeMinimalPom(final File bootstrapPomFile) throws IOException {
    final Model model = this.project.getModel();

    final MavenProject minimalProject = new MavenProject();
    final Model minModel = minimalProject.getModel();

    minModel.setGroupId(getBootstrapGroupId(this.project.getArtifact()));
    minModel.setArtifactId(model.getArtifactId());
    minModel.setVersion(model.getVersion());
    minModel.setName(model.getName());
    minModel.setPackaging("jar");
    minModel.setDescription(model.getDescription());
    minModel.setModelEncoding(model.getModelEncoding());
    minModel.setModelVersion(model.getModelVersion());
    minModel.setUrl(model.getUrl());
    minModel.setScm(model.getScm());
    minModel.setDevelopers(model.getDevelopers());
    minModel.setCiManagement(model.getCiManagement());
    minModel.setIssueManagement(model.getIssueManagement());

    minModel.setPrerequisites(model.getPrerequisites());
    minModel.setOrganization(model.getOrganization());
    minModel.setInceptionYear(model.getInceptionYear());
    minModel.setLicenses(model.getLicenses());

    final List<Dependency> dependencies = new ArrayList<Dependency>(model.getDependencies());
    // filter all of andromda dependencies away
    CollectionUtils.filter(dependencies, new Predicate() {
        public boolean evaluate(Object object) {
            Dependency dependency = (Dependency) object;
            final String lGroupId = dependency.getGroupId();
            return !lGroupId.startsWith("org.andromda") || lGroupId.startsWith("org.andromda.thirdparty");
        }
    });
    minModel.setDependencies(dependencies);

    final FileWriter fileWriter = new FileWriter(bootstrapPomFile);
    minimalProject.writeModel(fileWriter);
    fileWriter.flush();
}

From source file:org.ops4j.pax.construct.lifecycle.EclipseOSGiMojo.java

License:Apache License

/**
 * Download and save the Maven POM for the given artifact
 * /*from   w ww  .ja  v a  2s . com*/
 * @param baseDir base directory
 * @param artifact Maven artifact
 * @return the downloaded project
 */
private MavenProject writeProjectPom(File baseDir, Artifact artifact) {
    MavenProject pom = null;

    String groupId = artifact.getGroupId();
    String artifactId = artifact.getArtifactId();
    String version = artifact.getVersion();

    Artifact pomArtifact = artifactFactory.createProjectArtifact(groupId, artifactId, version);

    try {
        pom = m_mavenProjectBuilder.buildFromRepository(pomArtifact, remoteArtifactRepositories,
                localRepository);

        // need this when using Maven 2.1 which doesn't do any alignment
        m_pathTranslator.alignToBaseDirectory(pom.getModel(), baseDir);

        File pomFile = new File(baseDir, "pom.xml");

        Writer writer = StreamFactory.newXmlWriter(pomFile);
        pom.writeModel(writer);
        pom.setFile(pomFile);
        IOUtil.close(writer);
    } catch (ProjectBuildingException e) {
        getLog().warn("Unable to build POM for artifact " + pomArtifact);
    } catch (IOException e) {
        getLog().warn("Unable to write POM for artifact " + pomArtifact);
    }

    return pom;
}

From source file:org.ops4j.pax.construct.lifecycle.ProvisionMojo.java

License:Apache License

/**
 * Create new POM (based on the root POM) which lists the deployed bundles as dependencies
 * /*from   w ww  . ja  va 2  s .c  om*/
 * @param bundles list of bundles to be deployed
 * @return deployment project
 * @throws MojoExecutionException
 */
private MavenProject createDeploymentProject(List bundles) throws MojoExecutionException {
    MavenProject deployProject;

    if (null == m_project.getFile()) {
        deployProject = new MavenProject();
        deployProject.setGroupId("examples");
        deployProject.setArtifactId("pax-provision");
        deployProject.setVersion("1.0-SNAPSHOT");
    } else {
        deployProject = new MavenProject(m_project);
    }

    String internalId = PomUtils.getCompoundId(deployProject.getGroupId(), deployProject.getArtifactId());
    deployProject.setGroupId(internalId + ".build");
    deployProject.setArtifactId("deployment");

    // remove unnecessary cruft
    deployProject.setPackaging("pom");
    deployProject.getModel().setModules(null);
    deployProject.getModel().setDependencies(bundles);
    deployProject.getModel().setPluginRepositories(null);
    deployProject.getModel().setReporting(null);
    deployProject.setBuild(null);

    File deployFile = new File(deployProject.getBasedir(), "runner/deploy-pom.xml");

    deployFile.getParentFile().mkdirs();
    deployProject.setFile(deployFile);

    try {
        Writer writer = StreamFactory.newXmlWriter(deployFile);
        deployProject.writeModel(writer);
        IOUtil.close(writer);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to write deployment POM " + deployFile);
    }

    return deployProject;
}

From source file:org.phpmaven.plugin.pear.PearCreatePomMojo.java

License:Apache License

/**
 * @inheritDoc// w  w  w.ja  v a 2s .co m
 */
public void execute() throws MojoExecutionException {
    if (this.pearGroupId == null) {
        this.pearGroupId = this.channelToGroupId(this.pearChannelAlias);
    }
    if (this.pearArtifactId == null) {
        this.pearArtifactId = this.pearPackage;
    }

    final String[] versions = this.pearPackageVersion.split(",");
    final File path = this.pomTargetFile;

    if (versions.length > 1 && this.pearPackageMavenVersion != null) {
        throw new MojoExecutionException(
                "Multiple versions cannot be used with option pearPackageMavenVersion");
    }

    final File commonPom = new File(this.pomTargetFile, this.pearGroupId + "/pom.xml");
    if (!commonPom.exists()) {
        final StringBuffer modulesXmlBuffer = new StringBuffer();
        modulesXmlBuffer.append("    <modules>\n");
        modulesXmlBuffer.append("    </modules>\n");

        String pomXml = COMMON_XML_TEMPLATE.replace("${PEAR.CHANNEL}", this.pearGroupId.replace(".", "_"));
        pomXml = pomXml.replace("${TARGET.NAME}", this.pearName);
        pomXml = pomXml.replace("${REPOS.ID}", this.deployRepsitoryId);
        pomXml = pomXml.replace("${REPOS.NAME}", escapeXml(this.deployRepositoryName));
        pomXml = pomXml.replace("${REPOS.URL}", this.deployRepositoryUrl);
        pomXml = pomXml.replace("${SNAPSHOTS.ID}", this.snapshotsRepositoryId);
        pomXml = pomXml.replace("${SNAPSHOTS.NAME}", escapeXml(this.snapshotsRepositoryName));
        pomXml = pomXml.replace("${SNAPSHOTS.URL}", this.snapshotsRepositoryUrl);
        pomXml = pomXml.replace("${MODULES}", modulesXmlBuffer.toString());

        this.createPomFile(pomXml, commonPom);
    }

    try {
        final MavenProject commonProject = this.getProjectFromPom(commonPom);

        for (final String version : versions) {
            if (versions.length > 1 || this.pearPackageMavenVersion == null) {
                this.pearPackageMavenVersion = version;
            }
            this.pearPackageVersion = version;
            final String moduleName = this.pearPackage + "-" + this.pearPackageMavenVersion;
            this.pomTargetFile = new File(path, this.pearGroupId + "/" + moduleName + "/pom.xml");
            this.createThePom();
            final List<String> modules = commonProject.getModules();
            if (!modules.contains(moduleName)) {
                commonProject.getModel().addModule(moduleName);
                commonProject.writeModel(new FileWriter(commonPom));
            }
        }
    } catch (ProjectBuildingException ex) {
        throw new MojoExecutionException("Unable to read common pom " + commonPom, ex);
    } catch (IOException ex) {
        throw new MojoExecutionException("Unable to write common pom " + commonPom, ex);
    }
}

From source file:org.sonar.batch.MavenPluginsConfigurator.java

License:Open Source License

protected void savePom(Project project) {
    MavenProject pom = project.getPom();
    File targetPom = new File(project.getFileSystem().getSonarWorkingDirectory(), "sonar-pom.xml");
    FileWriter fileWriter = null;
    try {/*from  w w  w.java  2s. c o m*/
        fileWriter = new FileWriter(targetPom, false);
        pom.writeModel(fileWriter);

    } catch (IOException e) {
        throw new SonarException("Can not save pom to " + targetPom, e);
    } finally {
        IOUtils.closeQuietly(fileWriter);
    }
}

From source file:org.sonar.batch.phases.MavenPluginsConfigurator.java

License:Open Source License

protected void savePom(Project project) {
    MavenProject pom = project.getPom();
    if (pom != null) {
        File targetPom = new File(project.getFileSystem().getSonarWorkingDirectory(), "sonar-pom.xml");
        FileWriter fileWriter = null;
        try {/*from  w  w w .j a  v  a 2s  .c  om*/
            fileWriter = new FileWriter(targetPom, false);
            pom.writeModel(fileWriter);

        } catch (IOException e) {
            throw new SonarException("Can not save pom to " + targetPom, e);
        } finally {
            IOUtils.closeQuietly(fileWriter);
        }
    }
}

From source file:org.sonar.batch.scan.maven.MavenPluginsConfigurator.java

License:Open Source License

protected void savePom(Project project) {
    MavenProject pom = project.getPom();
    if (pom != null) {
        File targetPom = new File(project.getFileSystem().getSonarWorkingDirectory(), "sonar-pom.xml");
        FileWriter fileWriter = null;
        try {//from  www.java 2s .  c o m
            fileWriter = new FileWriter(targetPom, false);
            pom.writeModel(fileWriter);

        } catch (IOException e) {
            throw new IllegalStateException("Can not save pom to " + targetPom, e);
        } finally {
            IOUtils.closeQuietly(fileWriter);
        }
    }
}