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

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

Introduction

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

Prototype

public Model getModel() 

Source Link

Usage

From source file:org.wso2.developerstudio.eclipse.platform.ui.mvn.wizard.MvnMultiModuleWizard.java

License:Open Source License

public boolean performFinish() {
    // If the multiModuleProject is not empty, then this is thru UI. Just generate the POM
    MavenProject mavenProject = MavenUtils.createMavenProject(moduleModel.getGroupId(),
            moduleModel.getArtifactId(), moduleModel.getVersion(), "pom");

    if (moduleModel.isRequiredParent()) {
        Parent parent = new Parent();
        parent.setArtifactId(moduleModel.getParentArtifact());
        parent.setGroupId(moduleModel.getParentGroup());
        parent.setVersion(moduleModel.getParentVersion());
        String relativePath = moduleModel.getRelativePath();
        if (relativePath != null && !relativePath.trim().isEmpty()) {
            parent.setRelativePath(relativePath);
        }//from  ww  w .  j a  v a  2 s. c o  m
        mavenProject.getModel().setParent(parent);
    } else {
        mavenProject.getModel().setParent(null);
    }

    List modules = mavenProject.getModules();

    List<IProject> selectedProjects = moduleModel.getSelectedProjects();

    selectedProjects = sortProjects(selectedProjects);

    if (multiModuleProject != null) {
        IFile pomFile = multiModuleProject.getFile("pom.xml");
        if (pomFile.exists()) {
            // Parse the pom and see the packaging type
            try {
                MavenProject mavenProject2 = MavenUtils.getMavenProject(pomFile.getLocation().toFile());
                String packaging = mavenProject2.getPackaging();
                if (!"pom".equalsIgnoreCase(packaging)) {
                    addMavenModules(multiModuleProject, mavenProject, modules, selectedProjects, pomFile);
                } else {
                    modules = mavenProject2.getModules();
                    mavenProject2.setGroupId(moduleModel.getGroupId());
                    mavenProject2.setArtifactId(moduleModel.getArtifactId());
                    mavenProject2.setVersion(moduleModel.getVersion());
                    mavenProject2.getModel().setParent(mavenProject.getModel().getParent());
                    addMavenModules(multiModuleProject, mavenProject2, modules, selectedProjects, pomFile);
                }

            } catch (Exception e) {
                log.error("Error occured while trying to generate the Maven Project for the Project Pom", e);
            }

        } else {
            // Since pom is not there, just create the new pom with all the necessary things
            addMavenModules(multiModuleProject, mavenProject, modules, selectedProjects, pomFile);
        }
        //         Adding Maven Multi Module Nature to POM generated Project 
        addMavenMultiModuleProjectNature(multiModuleProject);

    } else {
        try {
            moduleModel.setProjectName(moduleModel.getArtifactId());
            project = createNewProject();

            addMavenMultiModuleProjectNature(project);

            addMavenModules(project, mavenProject, modules, selectedProjects, project.getFile("pom.xml"));

        } catch (CoreException e) {
            log.error("Error occured while creating the new Maven Multi Module Project", e);
        } catch (ObserverFailedException e) {
            log.error("Error occured while trying to inject values to the Project Model", e);
        }
    }
    return true;
}

From source file:org.wso2.developerstudio.eclipse.platform.ui.wizard.AbstractWSO2ProjectCreationWizard.java

License:Open Source License

public void createPOM(File pomLocation) throws Exception {
    MavenInfo mavenInfo = getModel().getMavenInfo();

    String customGroupId = preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
            GLOBAL_MAVEN_GROUP_ID, null, null);
    String customVersion = preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
            GLOBAL_MAVEN_VERSION, null, null);

    MavenProject mavenProject = MavenUtils.createMavenProject(
            customGroupId != null ? customGroupId : mavenInfo.getGroupId(), mavenInfo.getArtifactId(),
            customVersion != null ? customVersion : mavenInfo.getVersion(), mavenInfo.getPackageName());
    Parent parentProject = getModel().getMavenInfo().getParentProject();
    if (parentProject != null) {
        mavenProject.getModel().setParent(parentProject);
    }//from   w  w  w  .j  a  v  a2s  . c o  m
    String disableWSO2Repo = preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
            DISABLE_WSO2_REPOSITORY, null, null);
    if (disableWSO2Repo == null) {
        MavenUtils.updateMavenRepo(mavenProject);
    }
    Repository globalRepositoryFromPreference = getGlobalRepositoryFromPreference();

    if (globalRepositoryFromPreference != null) {
        mavenProject.getModel().addRepository(globalRepositoryFromPreference);
        mavenProject.getModel().addPluginRepository(globalRepositoryFromPreference);
    }

    MavenUtils.saveMavenProject(mavenProject, pomLocation);
}

From source file:org.wso2.developerstudio.eclipse.platform.ui.wizard.AbstractWSO2ProjectCreationWizard.java

License:Open Source License

public void createPOM(File pomLocation, String packagingType) throws Exception {
    MavenInfo mavenInfo = getModel().getMavenInfo();

    String customGroupId = preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
            GLOBAL_MAVEN_GROUP_ID, null, null);
    String customVersion = preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
            GLOBAL_MAVEN_VERSION, null, null);

    MavenProject mavenProject = MavenUtils.createMavenProject(
            customGroupId != null ? customGroupId : mavenInfo.getGroupId(), mavenInfo.getArtifactId(),
            customVersion != null ? customVersion : mavenInfo.getVersion(), packagingType);

    Parent parentProject = getModel().getMavenInfo().getParentProject();
    if (parentProject != null) {
        mavenProject.getModel().setParent(parentProject);
    }/*from   w  ww .jav a 2 s  .  c o m*/
    String disableWSO2Repo = preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
            DISABLE_WSO2_REPOSITORY, null, null);
    if (disableWSO2Repo == null) {
        MavenUtils.updateMavenRepo(mavenProject);
    }
    Repository globalRepositoryFromPreference = getGlobalRepositoryFromPreference();

    if (globalRepositoryFromPreference != null) {
        mavenProject.getModel().addRepository(globalRepositoryFromPreference);
        mavenProject.getModel().addPluginRepository(globalRepositoryFromPreference);
    }
    MavenUtils.saveMavenProject(mavenProject, pomLocation);
}

From source file:org.wso2.developerstudio.msf4j.artifact.project.nature.MSF4JArtifactProjectNature.java

License:Open Source License

/**
 * Update created pom.xml file with necessary dependencies and plug-ins so
 * that it works with WSO2 MSF4J server/*w w w  .  ja  va 2s  .  co  m*/
 * 
 * @throws IOException
 * @throws XmlPullParserException
 *
 */
private void updatePom(IProject project) throws IOException, XmlPullParserException {
    File mavenProjectPomLocation = project.getFile(POM_FILE).getLocation().toFile();
    MavenProject mavenProject = MavenUtils.getMavenProject(mavenProjectPomLocation);
    Parent msf4jParent = new Parent();
    msf4jParent.setGroupId(MSF4J_SERVICE_PARENT_GROUP_ID);
    msf4jParent.setArtifactId(MSF4J_SERVICE_PARENT_ARTIFACT_ID);
    msf4jParent.setVersion(MSF4JArtifactConstants.getMSF4JServiceParentVersion());
    mavenProject.getModel().setParent(msf4jParent);

    Properties generatedProperties = mavenProject.getModel().getProperties();
    generatedProperties.clear();

}

From source file:org.wso2.developerstudio.msf4j.artifact.util.MSF4JProjectImporter.java

License:Open Source License

private void saveMavenParentInfo(MavenProjectInfo projectInfo) throws IOException, XmlPullParserException {
    File mavenProjectPomLocation = projectInfo.getPomFile();// project.getFile(POM_FILE).getLocation().toFile();
    MavenProject mavenProject = null;
    mavenProject = MavenUtils.getMavenProject(mavenProjectPomLocation);
    Parent msf4jParent = new Parent();
    msf4jParent.setGroupId(MSF4J_SERVICE_PARENT_GROUP_ID);
    msf4jParent.setArtifactId(MSF4J_SERVICE_PARENT_ARTIFACT_ID);
    msf4jParent.setVersion(MSF4JArtifactConstants.getMSF4JServiceParentVersion());
    mavenProject.getModel().setParent(msf4jParent);

    Properties generatedProperties = mavenProject.getModel().getProperties();
    generatedProperties.clear();/*www . j  a v  a 2  s.  com*/

    mavenProject.getModel().addProperty(MSF4J_MAIN_CLASS_PROPERTY, DEFAULT_MAIN_CLASS_PROPERTY_VALUE);
    MavenUtils.saveMavenProject(mavenProject, mavenProjectPomLocation);
}

From source file:org.wso2.maven.capp.mojo.AbstractPOMGenMojo.java

License:Open Source License

protected void processArtifacts(List<Artifact> artifacts) throws MojoExecutionException {
    if (artifacts.isEmpty()) {
        if (getLog().isDebugEnabled()) {
            getLog().debug(System.currentTimeMillis() + " Artifacts list is empty. Nothing to process");
        }// ww w  .  j  ava2  s .co  m
        File projectLocation = new File(getOutputLocation() + "");

        projectLocation.mkdirs();
        setProjectLocation(projectLocation);
        getMavenModuleProject();
    } else {
        if (getLog().isDebugEnabled()) {
            getLog().info(
                    new Time(System.currentTimeMillis()) + " Artifacts list is not empty. Start processing");
        }
        for (Artifact artifact : artifacts) {
            if (artifact.getType().equalsIgnoreCase(getArtifactType())) {

                if (getLog().isDebugEnabled()) {
                    getLog().debug(
                            new Time(System.currentTimeMillis()) + " Creating maven project for artifact ");
                }

                getLog().info("Creating maven project for artifact " + artifact.getName() + ":"
                        + artifact.getVersion() + "...");

                if (getLog().isDebugEnabled()) {
                    getLog().debug(
                            new Time(System.currentTimeMillis()) + " Trying to generate the Maven Project...");
                }

                getLog().info("\tgenerating maven project...");

                File projectLocation = new File(getOutputLocation() + File.separator + getArtifactPostFix(),
                        artifact.getName());

                projectLocation.mkdirs();
                setProjectLocation(projectLocation);

                // This will be null if the artifact is referencing to a
                // workspace project.
                MavenProject artifactMavenProject = createMavenProjectForCappArtifact(artifact, artifacts,
                        projectLocation);
                artifactMavenProject.setDistributionManagement(project.getDistributionManagement());

                if (getLog().isDebugEnabled()) {
                    getLog().debug(
                            new Time(System.currentTimeMillis()) + " Maven Project generation completed");
                }

                if (artifactMavenProject != null) {
                    if (getLog().isDebugEnabled()) {
                        getLog().debug(
                                new Time(System.currentTimeMillis()) + " Generated Maven project is not null");
                    }

                    try {
                        if (getLog().isDebugEnabled()) {
                            getLog().debug(new Time(System.currentTimeMillis()) + " copying resources...");
                        }

                        getLog().info("\tcopying resources...");
                        String artifactAsMavenModule = CAppMavenUtils
                                .getMavenModuleRelativePath(getModuleProject(), projectLocation);
                        List existingModules = getMavenModuleProject().getModules();
                        if (!existingModules.contains(artifactAsMavenModule)) {
                            existingModules.add(artifactAsMavenModule);
                        }

                        if (getLog().isDebugEnabled()) {
                            getLog().debug(new Time(System.currentTimeMillis())
                                    + " Module list is updated with new module.");
                        }
                        //                     Repository repo = new Repository();
                        //                     repo.setUrl("http://dist.wso2.org/maven2");
                        //                     repo.setId("wso2-maven2-repository-1");
                        //                     artifactMavenProject.getModel().addRepository(repo);
                        //                     artifactMavenProject.getModel()
                        //                           .addPluginRepository(repo);

                        //Add repositories defined in the parent project to the generated poms.
                        List repositories = project.getRepositories();
                        artifactMavenProject.getModel().getRepositories().addAll(repositories);
                        artifactMavenProject.getModel().getPluginRepositories().addAll(repositories);

                        if (getLog().isDebugEnabled()) {
                            getLog().debug(new Time(System.currentTimeMillis())
                                    + " Maven project successfully updated with Repositories");
                        }

                        CAppMavenUtils.saveMavenProject(artifactMavenProject,
                                new File(projectLocation, "pom.xml"));
                        CAppMavenUtils.saveMavenProject(getMavenModuleProject(), getModuleProject());

                        if (getLog().isDebugEnabled()) {
                            getLog().debug(new Time(System.currentTimeMillis())
                                    + " Maven projects successfully saved");
                        }

                        copyResources(artifactMavenProject, projectLocation, artifact);
                    } catch (Exception e) {
                        throw new MojoExecutionException(
                                "Error creating maven project for artifact '" + artifact.getName() + "'", e);
                    }
                }
            }
        }
    }
}

From source file:org.wso2.maven.core.utils.MavenUtils.java

License:Open Source License

public static void saveMavenProject(MavenProject project, File file) throws Exception {
    MavenXpp3Writer mavenXpp3writer = new MavenXpp3Writer();
    mavenXpp3writer.write(new FileWriter(file), project.getModel());
}

From source file:org.wso2.maven.plugin.jaxws.JaxWSPOMGenMojo.java

License:Open Source License

protected void addPlugins(MavenProject artifactMavenProject, Artifact artifact) {
    if (artifact.getFile().getPath().endsWith(".jar")) {
        Plugin plugin = CAppMavenUtils.createPluginEntry(artifactMavenProject, "org.wso2.maven",
                "maven-jaxws-plugin", WSO2MavenPluginConstantants.MAVEN_JAXWS_PLUGIN_VERSION, true);
        //   Plugin plugin = CAppMavenUtils.createPluginEntry(artifactMavenProject,"org.apache.maven.plugins","maven-jar-plugin","2.3.1",true);
        Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
        //add configuration
        Xpp3Dom aritfact = CAppMavenUtils.createConfigurationNode(configuration, "artifact");
        aritfact.setValue(artifact.getFile().getName());
    } else {/*from   w  w w .j av a2s . c  o  m*/
        artifactMavenProject.setPackaging("jar");
        artifactMavenProject.getModel().addProperty(CAppUtils.PROPERTY_CAPP_TYPE, getArtifactType());
        getLog().info("Adding JAXWS reated plugins");

        Plugin plugin = CAppMavenUtils.createPluginEntry(artifactMavenProject, "org.apache.maven.plugins",
                "maven-dependency-plugin", "2.2", true);
        PluginExecution execution = new PluginExecution();
        execution.setId("unpack");
        execution.setPhase("package");
        List<String> goals = new ArrayList<String>();
        goals.add("unpack");
        execution.setGoals(goals);
        plugin.addExecution(execution);
        Xpp3Dom config = (Xpp3Dom) plugin.getConfiguration();
        Xpp3Dom artifactItems = CAppMavenUtils.createConfigurationNode(config, "artifactItems");
        Xpp3Dom artifactItem = CAppMavenUtils.createConfigurationNode(artifactItems, "artifactItem");
        List<ProjectMapping> projectMappings;
        String serviceProject = null;
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(artifact.getFile()));
            serviceProject = properties.getProperty("Projects");
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            projectMappings = getProjectMappings();

            for (int i = 0; i < projectMappings.size(); i++) {
                ProjectMapping projectMapping = projectMappings.get(i);

                if (projectMapping.projectName.equalsIgnoreCase(serviceProject)) {
                    Xpp3Dom groupId = CAppMavenUtils.createConfigurationNode(artifactItem, "groupId");
                    groupId.setValue(projectMapping.getGroupId());
                    Xpp3Dom artifactId = CAppMavenUtils.createConfigurationNode(artifactItem, "artifactId");
                    artifactId.setValue(projectMapping.getArtifactId());
                    Xpp3Dom version = CAppMavenUtils.createConfigurationNode(artifactItem, "version");
                    version.setValue(projectMapping.getVersion());
                    Xpp3Dom type = CAppMavenUtils.createConfigurationNode(artifactItem, "type");
                    type.setValue("jar");
                    Xpp3Dom overWrite = CAppMavenUtils.createConfigurationNode(artifactItem, "overWrite");
                    overWrite.setValue("true");
                    Xpp3Dom outputDirectory = CAppMavenUtils.createConfigurationNode(artifactItem,
                            "outputDirectory");
                    outputDirectory.setValue("target" + File.separator + "jar");
                }
            }
        } catch (Exception e) {
            getLog().error(e);
        }
        //Adding Jar plugin entry to build section
        Plugin pluginJaxWs = CAppMavenUtils.createPluginEntry(artifactMavenProject, "org.apache.maven.plugins",
                "maven-jar-plugin", "2.3.1", true);
        PluginExecution executionJaxWs = new PluginExecution();
        executionJaxWs.setId("jar");
        executionJaxWs.setPhase("package");
        List<String> goalsJaxWs = new ArrayList<String>();
        goalsJaxWs.add("jar");
        executionJaxWs.setGoals(goalsJaxWs);
        Xpp3Dom mainNode = MavenUtils.createMainConfigurationNode();
        pluginJaxWs.setConfiguration(mainNode);
        Xpp3Dom classDirNode = MavenUtils.createConfigurationNode(mainNode, "classesDirectory");
        classDirNode.setValue("target" + File.separator + "jar");
        Xpp3Dom includes = MavenUtils.createConfigurationNode(mainNode, "includes");
        Xpp3Dom include = MavenUtils.createConfigurationNode(includes, "include");
        include.setValue("**/*.class");
        Xpp3Dom forceCreation = MavenUtils.createConfigurationNode(mainNode, "forceCreation");
        forceCreation.setValue("true");
        pluginJaxWs.setConfiguration(mainNode);
        pluginJaxWs.addExecution(executionJaxWs);
    }
}

From source file:org.xwiki.contrib.maven.MavenPackagerUtils.java

License:Open Source License

public Extension toExtension(Artifact artifact, Collection<Exclusion> exclusions)
        throws MojoExecutionException {
    MavenProject project = getMavenProject(artifact);
    Model model = project.getModel();

    DefaultLocalExtension extension = new DefaultLocalExtension(null,
            new ExtensionId(artifact.getGroupId() + ':' + artifact.getArtifactId(), artifact.getBaseVersion()),
            artifact.getType());/*from  w  ww.j  ava 2  s.c  o  m*/

    extension.setName(getPropertyString(model, PackageExtensionsMojo.MPNAME_NAME, model.getName()));
    extension
            .setSummary(getPropertyString(model, PackageExtensionsMojo.MPNAME_SUMMARY, model.getDescription()));
    extension.setWebsite(getPropertyString(model, PackageExtensionsMojo.MPNAME_WEBSITE, model.getUrl()));

    // authors
    for (Developer developer : model.getDevelopers()) {
        URL authorURL = null;
        if (developer.getUrl() != null) {
            try {
                authorURL = new URL(developer.getUrl());
            } catch (MalformedURLException e) {
                // TODO: log ?
            }
        }

        extension.addAuthor(new DefaultExtensionAuthor(
                StringUtils.defaultIfBlank(developer.getName(), developer.getId()), authorURL));
    }

    // licenses
    if (!model.getLicenses().isEmpty()) {
        for (License license : model.getLicenses()) {
            extension.addLicense(getExtensionLicense(license));
        }
    }

    // features
    String featuresString = getProperty(model, PackageExtensionsMojo.MPNAME_FEATURES);
    if (StringUtils.isNotBlank(featuresString)) {
        featuresString = featuresString.replaceAll("[\r\n]", "");
        extension.setFeatures(converter.<Collection<String>>convert(List.class, featuresString));
    }

    // dependencies
    for (Dependency mavenDependency : model.getDependencies()) {
        if (!mavenDependency.isOptional() && (mavenDependency.getScope().equals("compile")
                || mavenDependency.getScope().equals("runtime"))) {
            boolean excluded = false;
            for (Exclusion exclusion : exclusions) {
                if (mavenDependency.getArtifactId().equals(exclusion.getArtifactId())
                        && mavenDependency.getGroupId().equals(exclusion.getGroupId())) {
                    excluded = true;
                    break;
                }
            }
            if (!excluded) {
                extension.addDependency(new DefaultExtensionDependency(
                        mavenDependency.getGroupId() + ':' + mavenDependency.getArtifactId(),
                        new DefaultVersionConstraint(mavenDependency.getVersion())));
            }
        }
    }

    extension.setFile(artifact.getFile());
    return extension;
}

From source file:org.xwiki.tool.enforcer.AbstractPomCheck.java

License:Open Source License

/**
 * The resolved Maven model (i.e. with parent poms taken into account).
 *
 * @param helper the enforcer helper object
 * @return the resolved Model instance for the current Maven project (this contains the data from the pom.xml file
 *         after interpolation)/*  w  w  w .  j  a  v a 2 s  .c om*/
 * @throws EnforcerRuleException if an error occurred getting the Model instance
 */
protected Model getResolvedModel(EnforcerRuleHelper helper) throws EnforcerRuleException {
    MavenProject project = getMavenProject(helper);
    // Note: the model is resolved at this point, which means the Model contains
    return project.getModel();
}