Search in sources :

Example 1 with ModelETLRequest

use of org.apache.maven.model.jdom.etl.ModelETLRequest in project CodenameOne by codenameone.

the class GenerateAppProjectMojo method injectDependencies.

private void injectDependencies() throws MojoExecutionException {
    if (!generateAppProjectConfigFile().exists()) {
        return;
    }
    try {
        Properties props = new Properties();
        new RichPropertiesReader().load(generateAppProjectConfigFile(), props);
        String dependencies = props.getProperty("dependencies");
        String parentDependencies = props.getProperty("parentDependencies");
        if (targetCommonPomXml().exists()) {
            Model model;
            ModelETL modelETL;
            /*
                try (FileInputStream fis = new FileInputStream(targetCommonPomXml())){
                    MavenXpp3Reader reader = new MavenXpp3Reader();
                    model = reader.read(fis);

                } catch (Exception ex) {
                    throw new MojoExecutionException("Failed to read pom.xml file from "+targetCommonPomXml(), ex);
                }
                */
            try {
                ModelETLRequest modelETLRequest = new ModelETLRequest();
                modelETL = new JDomModelETLFactory().newInstance(modelETLRequest);
                modelETL.extract(targetCommonPomXml());
                model = modelETL.getModel();
            } catch (Exception ex) {
                throw new MojoExecutionException("Failed to read pom.xml file from " + targetCommonPomXml(), ex);
            }
            if (dependencies != null) {
                String dummyModelStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + "    <modelVersion>4.0.0</modelVersion>\n" + "    <groupId>link.sharpe</groupId>\n" + "    <artifactId>mavenproject1</artifactId>\n" + "    <version>1.0-SNAPSHOT</version>\n" + "    <dependencies>\n" + dependencies + "    </dependencies>\n" + "</project>";
                Model dummyModel;
                try {
                    MavenXpp3Reader reader = new MavenXpp3Reader();
                    dummyModel = reader.read(new CharArrayReader(dummyModelStr.toCharArray()));
                } catch (Exception ex) {
                    throw new MojoExecutionException("Failed to read dummy pom.xml file while injecting dependencies into " + targetCommonPomXml(), ex);
                }
                for (Dependency dep : dummyModel.getDependencies()) {
                    model.addDependency(dep);
                }
                /*
                    MavenXpp3Writer writer = new MavenXpp3Writer();
                    try (FileOutputStream fos = new FileOutputStream(targetCommonPomXml())) {
                        writer.write(fos, model);
                    }
                    */
                modelETL.load(targetCommonPomXml());
            }
        }
        if (targetRootPomXml().exists()) {
            Model model;
            ModelETL modelETL;
            /*
                try (FileInputStream fis = new FileInputStream(targetRootPomXml())){
                    MavenXpp3Reader reader = new MavenXpp3Reader();
                    model = reader.read(fis);

                } catch (Exception ex) {
                    throw new MojoExecutionException("Failed to read pom.xml file from "+targetRootPomXml(), ex);
                }

                 */
            try {
                ModelETLRequest modelETLRequest = new ModelETLRequest();
                modelETL = new JDomModelETLFactory().newInstance(modelETLRequest);
                modelETL.extract(targetRootPomXml());
                model = modelETL.getModel();
            } catch (Exception ex) {
                throw new MojoExecutionException("Failed to read pom.xml file from " + targetRootPomXml(), ex);
            }
            if (parentDependencies != null) {
                // getLog().info("Parent dependencies: "+parentDependencies);
                String dummyModelStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + "    <modelVersion>4.0.0</modelVersion>\n" + "    <groupId>link.sharpe</groupId>\n" + "    <artifactId>mavenproject1</artifactId>\n" + "    <version>1.0-SNAPSHOT</version>\n" + "    <dependencies>\n" + parentDependencies + "    </dependencies>\n" + "</project>";
                Model dummyModel;
                try {
                    MavenXpp3Reader reader = new MavenXpp3Reader();
                    dummyModel = reader.read(new CharArrayReader(dummyModelStr.toCharArray()));
                } catch (Exception ex) {
                    throw new MojoExecutionException("Failed to read dummy pom.xml file while injecting dependencies into " + targetRootPomXml(), ex);
                }
                for (Dependency dep : dummyModel.getDependencies()) {
                    model.addDependency(dep);
                }
                /*
                    MavenXpp3Writer writer = new MavenXpp3Writer();
                    try (FileOutputStream fos = new FileOutputStream(targetRootPomXml())) {
                        writer.write(fos, model);
                    }

                     */
                modelETL.load(targetRootPomXml());
            }
        }
    } catch (IOException ex) {
        throw new MojoExecutionException("Failed to process configuration for generateAppProjectConfigFile " + generateAppProjectConfigFile(), ex);
    } catch (RichPropertiesReader.ConfigSyntaxException ex) {
        throw new MojoExecutionException("Failed to process configuration for generateAppProjectConfigFile " + generateAppProjectConfigFile(), ex);
    }
}
Also used : ModelETL(org.apache.maven.model.jdom.etl.ModelETL) JDomModelETL(org.apache.maven.model.jdom.etl.JDomModelETL) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) Dependency(org.apache.maven.model.Dependency) SortedProperties(com.codename1.ant.SortedProperties) MavenExecutionException(org.apache.maven.MavenExecutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ModelETLRequest(org.apache.maven.model.jdom.etl.ModelETLRequest) RichPropertiesReader(com.codename1.util.RichPropertiesReader) JDomModelETLFactory(org.apache.maven.model.jdom.etl.JDomModelETLFactory) Model(org.apache.maven.model.Model)

Example 2 with ModelETLRequest

use of org.apache.maven.model.jdom.etl.ModelETLRequest in project CodenameOne by codenameone.

the class UpdateCodenameOneMojo method executeImpl.

@Override
protected void executeImpl() throws MojoExecutionException, MojoFailureException {
    if (!isCN1ProjectDir()) {
        return;
    }
    updateCodenameOne(true);
    String existingCn1Version = project.getModel().getProperties().getProperty("cn1.version");
    String existingCn1PluginVersion = project.getModel().getProperties().getProperty("cn1.plugin.version");
    boolean isAutoVersion = false;
    if (newVersion == null || newVersion.isEmpty()) {
        if (!existingCn1Version.endsWith("-SNAPSHOT")) {
            // As long as the existing version is not a snapshot, we'll update to the latest in Maven
            // by default.
            newVersion = "LATEST";
            isAutoVersion = true;
        }
    }
    if ("LATEST".equals(newVersion)) {
        try {
            newVersion = findLatestVersionOnMavenCentral();
        } catch (Exception ex) {
            getLog().error("Failed to find latest version from Maven central", ex);
        }
    }
    getLog().info("Existing cn1.version=" + existingCn1Version);
    getLog().info("Existing cn1.plugin.version=" + existingCn1PluginVersion);
    if (newVersion != null && !newVersion.isEmpty() && (!newVersion.equals(existingCn1Version) || !newVersion.equals(existingCn1PluginVersion))) {
        getLog().info("Attempting to update project to version " + newVersion);
        // MavenXpp3Reader pomReader = new MavenXpp3Reader();
        Model model = null;
        ModelETL modelETL;
        File pomFile = new File(project.getParent().getBasedir(), "pom.xml");
        /*
            try (FileInputStream fis = new FileInputStream(pomFile)) {
                model = pomReader.read(new InputStreamReader(fis, "UTF-8"), false);
            } catch (Exception ex) {
                getLog().error("Failed to load pom.xml file from parent project", ex);
                throw new MojoExecutionException("Failed to read pom.xml file", ex);
            }

             */
        try {
            ModelETLRequest modelETLRequest = new ModelETLRequest();
            modelETL = new JDomModelETLFactory().newInstance(modelETLRequest);
            modelETL.extract(pomFile);
            model = modelETL.getModel();
        } catch (Exception ex) {
            getLog().error("Failed to load pom.xml file from parent project", ex);
            throw new MojoExecutionException("Failed to read pom.xml file", ex);
        }
        boolean changed = false;
        if (!isAutoVersion || !existingCn1Version.endsWith("-SNAPSHOT")) {
            if (!existingCn1Version.equals(newVersion)) {
                getLog().info("Setting cn1.version=" + newVersion);
                model.getProperties().setProperty("cn1.version", newVersion);
                changed = true;
            } else {
                getLog().info("cn1.version already up to date.  Not changing");
            }
        } else {
            getLog().warn("Not updating cn1.version because current version is a snapshot.  To update cn1.version property run mvn cn1:update -DnewVersion=XXXX");
        }
        if (!isAutoVersion || !existingCn1PluginVersion.endsWith("-SNAPSHOT")) {
            if (!existingCn1PluginVersion.equals(newVersion)) {
                getLog().info("Setting cn1.plugin.version=" + newVersion);
                model.getProperties().setProperty("cn1.plugin.version", newVersion);
                changed = true;
            } else {
                getLog().info("cn1.plugin.version already up to date. Not changing.");
            }
        } else {
            getLog().warn("Not updating cn1.plugin.version because current version is a snapshot.  To update cn1.plugin.version property, run mvn cn1:update -DnewVersion=XXX");
        }
        if (changed) {
            try {
                FileUtils.copyFile(pomFile, new File(pomFile.getParentFile(), "pom.xml.bak"));
            } catch (Exception ex) {
                throw new MojoExecutionException("Failed to back up pom.xml file", ex);
            }
            /*
                try (FileOutputStream fos = new FileOutputStream(pomFile)) {
                    MavenXpp3Writer pomWriter = new MavenXpp3Writer();
                    getLog().info("Updating "+pomFile+" with new cn1.version and cn1.plugin.version properties");

                    pomWriter.write(fos, model);


                } catch (IOException e) {
                    getLog().error("Failed to write changes to the pom file", e);
                    throw new MojoExecutionException("Failed to write canges to the pom file.", e);
                }

                 */
            try {
                modelETL.load(pomFile);
            } catch (IOException e) {
                getLog().error("Failed to write changes to the pom file", e);
                throw new MojoExecutionException("Failed to write canges to the pom file.", e);
            }
        }
    } else {
        if (newVersion == null || newVersion.isEmpty()) {
            getLog().warn("Not updating pom.xml file because it is currently set to use a SNAPSHOT version of Codename One.");
            getLog().info("To update to a newer version of CN1 in maven use the -DnewVersion property.");
            getLog().info("e.g. -DnewVersion=LATEST to update to the latest version in Maven central");
            getLog().info("or -DnewVersion=7.0.12, for example");
        } else {
            getLog().info("Maven version already up to date.  Not updating pom.xml file");
        }
    }
}
Also used : ModelETL(org.apache.maven.model.jdom.etl.ModelETL) ModelETLRequest(org.apache.maven.model.jdom.etl.ModelETLRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JDomModelETLFactory(org.apache.maven.model.jdom.etl.JDomModelETLFactory) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Aggregations

Model (org.apache.maven.model.Model)2 JDomModelETLFactory (org.apache.maven.model.jdom.etl.JDomModelETLFactory)2 ModelETL (org.apache.maven.model.jdom.etl.ModelETL)2 ModelETLRequest (org.apache.maven.model.jdom.etl.ModelETLRequest)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 SortedProperties (com.codename1.ant.SortedProperties)1 RichPropertiesReader (com.codename1.util.RichPropertiesReader)1 MavenExecutionException (org.apache.maven.MavenExecutionException)1 Dependency (org.apache.maven.model.Dependency)1 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)1 JDomModelETL (org.apache.maven.model.jdom.etl.JDomModelETL)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1