Java Code Examples for org.netbeans.modules.maven.model.pom.POMModel#getProject()

The following examples show how to use org.netbeans.modules.maven.model.pom.POMModel#getProject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static Dependency checkModelDependency(POMModel pom, String groupId, String artifactId, boolean add) {
    Project mdl = pom.getProject();
    Dependency ret = mdl.findDependencyById(groupId, artifactId, null);
    Dependency managed = null;
    if (ret == null || ret.getVersion() == null) {
        //check dependency management section as well..
        DependencyManagement mng = mdl.getDependencyManagement();
        if (mng != null) {
            managed = mng.findDependencyById(groupId, artifactId, null);
        }
    }
    if (add && ret == null) {
        ret = mdl.getModel().getFactory().createDependency();
        ret.setGroupId(groupId);
        ret.setArtifactId(artifactId);
        mdl.addDependency(ret);
    }
    // if managed dependency section is present, return that one for editing..
    return managed == null ? ret : managed;
}
 
Example 2
Source File: ModelUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Opens pom at a plugin with the given groupId and artifactId.
 * 
 * @param model the model to open
 * @param groupId the plugin groupId
 * @param artifactId the plugin artifactId
 */
public static void openAtPlugin(POMModel model, String groupId, String artifactId) {
    int pos = -1;
    org.netbeans.modules.maven.model.pom.Project p = model.getProject();
    Build bld = p.getBuild();
    if (bld != null) {
        Plugin plg = bld.findPluginById(groupId, artifactId);
        if (plg != null) {
            pos = plg.findPosition();
        }
    }    

    if(pos == -1) {
        pos = p.findPosition();
    }

    if(pos == -1) {
        return;
    }        
    openAtPosition(model, pos);
}
 
Example 3
Source File: AddMavenCompilerPlugin.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void performOperation(final POMModel model) {
    factory = model.getFactory();
    project = model.getProject();
    Build build = project.getBuild();
    if (build == null) {
        build = factory.createBuild();
        project.setBuild(build);
    }

    Plugin plugin = searchMavenCompilerPlugin(build);
    if (plugin == null) {
        build.addPlugin(createMavenEclipseCompilerPlugin());
    } else {
        Plugin newPlugin = createMavenEclipseCompilerPlugin(plugin);

        build.removePlugin(plugin);
        build.addPlugin(newPlugin);
    }
}
 
Example 4
Source File: AddGroovyEclipseCompiler.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void performOperation(POMModel model) {
    POMComponentFactory factory = model.getFactory();
    Project project = model.getProject();

    Build build = project.getBuild();
    if (build == null) {
        build = factory.createBuild();
        project.setBuild(build);
    }

    Plugin groovyEclipseCompiler = factory.createPlugin();
    groovyEclipseCompiler.setGroupId(MavenConstants.GROOVY_ECLIPSE_COMPILER_GROUP_ID);
    groovyEclipseCompiler.setArtifactId(MavenConstants.GROOVY_ECLIPSE_COMPILER_ARTIFACT_ID);
    groovyEclipseCompiler.setVersion(MavenConstants.GROOVY_ECLIPSE_COMPILER_VERSION);
    groovyEclipseCompiler.setExtensions(Boolean.TRUE); // THIS IS THE IMPORTANT PART !

    if (!groovyEclipseCompilerExists(build)) {
        build.addPlugin(groovyEclipseCompiler);
    }
}
 
Example 5
Source File: NetBeansRunParamsIDEChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static ModelOperation<POMModel> createRemoveIdePropertyOperation() {
  return new ModelOperation<POMModel>() {
        public @Override void performOperation(POMModel model) {
            Project project = model.getProject();
            Properties properties = project.getProperties();
            if (properties != null) {
                if (properties.getProperty(OLD_PROPERTY) != null) {
                    properties.setProperty(OLD_PROPERTY, null);
                }
                String args = properties.getProperty(MASTER_PROPERTY);
                if (args != null) {
                    String ref = "${" + OLD_PROPERTY + "}"; // NOI18N
                    if (args.contains(ref)) {
                        args = args.replace(ref, "");
                        if (args.trim().length() == 0) {
                            args = null;
                        }
                        properties.setProperty(MASTER_PROPERTY, args);
                    }
                }
            }
        }
  };
}
 
Example 6
Source File: NetBeansRunParamsIDEChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static ModelOperation<POMModel> createUpgradePluginOperation() {
    return new ModelOperation<POMModel>() {
        public @Override
        void performOperation(POMModel model) {
            POMComponentFactory factory = model.getFactory();
            Project project = model.getProject();
            Build bld = project.getBuild();
            if (bld == null) {
                bld = factory.createBuild();
                project.setBuild(bld);
            }
            Plugin plg = PluginBackwardPropertyUtils.findPluginFromBuild(bld);
            if (plg == null) {
                plg = factory.createPlugin();
                plg.setGroupId(MavenNbModuleImpl.GROUPID_APACHE);
                plg.setArtifactId(MavenNbModuleImpl.NBM_PLUGIN);
                plg.setExtensions(Boolean.TRUE);
                bld.addPlugin(plg);
            }
            plg.setVersion(MavenNbModuleImpl.getLatestNbmPluginVersion()); //
        }
    };
}
 
Example 7
Source File: SourcesPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void performOperation(POMModel model) {
    String s = PluginPropertyUtils.getPluginProperty(handle.getProject(), Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER, "source", null, null);
    String t = PluginPropertyUtils.getPluginProperty(handle.getProject(), Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER, "source", null, null);
    if (s == null && t == null) {
        Project p = model.getProject();
        if (p != null) {
            Properties prop = p.getProperties();
            if (prop == null) {
                prop = model.getFactory().createProperties();
                p.setProperties(prop);
            }
            prop.setProperty("maven.compiler.source", sourceLevel);
            prop.setProperty("maven.compiler.target", sourceLevel);
        }
    } else {
        ModelUtils.setSourceLevel(model, sourceLevel);
    }
}
 
Example 8
Source File: EnablePreviewMavenProj.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void performOperation(final POMModel model) {
    factory = model.getFactory();
    org.netbeans.modules.maven.model.pom.Project proj = model.getProject();
    Build build = model.getProject().getBuild();
    if (build == null) {
        build = factory.createBuild();
        proj.setBuild(build);
    }

    Plugin oldPlugin = searchMavenCompilerPlugin(build);
    if (oldPlugin == null) {
        build.addPlugin(createMavenCompilerPlugin());
    } else {

        Plugin newPlugin = updateMavenCompilerPlugin(oldPlugin);

        build.removePlugin(oldPlugin);
        build.addPlugin(newPlugin);
    }
}
 
Example 9
Source File: ModelTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testModelWrite() throws Exception {
    ModelSource source = createModelSource("sample.pom");
    try {
        POMModel model = POMModelFactory.getDefault().getModel(source);
        assertNotNull(model.getRootComponent());
        Project prj = model.getProject();

        Parent parent = prj.getPomParent();
        assertNotNull(parent);
        assertNotNull(parent.getGroupId());
        assertEquals("org.codehaus.mojo", parent.getGroupId());

        model.startTransaction();
        parent.setGroupId("foo.bar");
        model.endTransaction();
        assertEquals("foo.bar", parent.getGroupId());

        //this test fails here.. cannot rollback single property changes..
        model.startTransaction();
        parent.setGroupId("bar.foo");
        model.rollbackTransaction();
        
        assertEquals("foo.bar", parent.getGroupId());

    } finally {
        File file = source.getLookup().lookup(File.class);
        file.deleteOnExit();
    }
}
 
Example 10
Source File: AddOSGiParamToNbmPluginConfiguration.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void performOperation(POMModel model) {
    Project p = model.getProject();
    Build bld = p.getBuild();
    if (bld == null) {
        bld = model.getFactory().createBuild();
        p.setBuild(bld);
    }
    Plugin plg = PluginBackwardPropertyUtils.findPluginFromBuild(bld);
    if (plg == null) {
        //how come the plugin is not there? maybe using on wrong project?
        //check plugin management first.
        PluginManagement pm = bld.getPluginManagement();
        if (pm != null) {
            plg = PluginBackwardPropertyUtils.findPluginFromPluginManagement(pm);
        }
        if (plg == null) { // should not happen to begin with
            plg = model.getFactory().createPlugin();
            bld.addPlugin(plg);
            plg.setGroupId(MavenNbModuleImpl.GROUPID_APACHE);
            plg.setArtifactId(MavenNbModuleImpl.NBM_PLUGIN);
            plg.setVersion(MavenNbModuleImpl.getLatestNbmPluginVersion());
        }
    }
    Configuration cnf = plg.getConfiguration();
    if (cnf == null) {
        cnf = model.getFactory().createConfiguration();
        plg.setConfiguration(cnf);
    }
    cnf.setSimpleParameter("useOSGiDependencies", Boolean.toString(useOsgi));
}
 
Example 11
Source File: DependencyGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private DependencyContainer findContainer(int pos, POMModel model) {
    Component dc = model.findComponent(pos);
    while (dc != null) {
        if (dc instanceof DependencyContainer) {
            return (DependencyContainer) dc;
        }
        dc = dc.getParent();
    }
    return model.getProject();
}
 
Example 12
Source File: ActionProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void performOperation(POMModel model) {
    org.netbeans.modules.maven.model.pom.Project prj = model.getProject();
    if (newJUnit != null) {
        findDependency("junit", "junit", newJUnit, prj);
    }
    if (newSurefirePluginVersion != null) {
        ModelUtils.updatePluginVersion(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_SUREFIRE, newSurefirePluginVersion, prj);
    }
    ModelUtils.openAtPlugin(model, Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_SUREFIRE);
}
 
Example 13
Source File: CreateProjectBuilder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void performOperation(POMModel model) {
    if (relPath != null && model.getProject() != null) {
        List<String> modules = model.getProject().getModules();
        if (modules == null || !modules.contains(relPath)) {
            model.getProject().addModule(relPath);
        }
    }
}
 
Example 14
Source File: ActionProviderImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void performOperation(POMModel model) {
    org.netbeans.modules.maven.model.pom.Project prj = model.getProject();
    ModelUtils.updatePluginVersion(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER, "3.6.1", prj);
    ModelUtils.openAtPlugin(model, Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER);
}
 
Example 15
Source File: CreateProjectBuilder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void performOperation(POMModel model) {
    org.netbeans.modules.maven.model.pom.Project root = model.getProject();
    if (root != null) {
        MavenProject parent = context.getParent();
        if (parent != null) {
            Parent parentpom = model.getFactory().createParent();
            parentpom.setGroupId(parent.getGroupId());
            parentpom.setArtifactId(parent.getArtifactId());
            parentpom.setVersion(parent.getVersion());
            File pfile = FileUtil.normalizeFile(parent.getFile());
            String rel = FileUtilities.relativizeFile(context.getProjectDirectory(), pfile);
            if (rel != null) {
                if ("..".equals(rel) || "../pom.xml".equals(rel)) {
                    
                } else {
                    parentpom.setRelativePath(rel);
                }
            } else {
                parentpom.setRelativePath("");
            }
            root.setPomParent(parentpom);
            
        }
        if (parent == null || !context.getGroupId().equals(parent.getGroupId())) {
            root.setGroupId(context.getGroupId());
        }
        root.setArtifactId(context.getArtifactId());
        if (parent == null || !context.getVersion().equals(parent.getVersion())) {
            root.setVersion(context.getVersion());
        }
        root.setPackaging(packaging);
        
        boolean setEncoding = true;
        if (parent != null) {
            java.util.Properties parentprops = parent.getProperties();
            if (parentprops != null && parentprops.containsKey("project.build.sourceEncoding")) {
                setEncoding = false;
            }
        }
        if (setEncoding) {
            Properties props = root.getProperties();
            if (props == null) {
                props = model.getFactory().createProperties();
                root.setProperties(props);
            }
            props.setProperty("project.build.sourceEncoding", "UTF-8");
        }
    }
}
 
Example 16
Source File: InjectSpringBootGenerator.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
@Override
protected int pomInvoke(POMModel model, int caretPosition) throws Exception {
    Project prj = model.getProject();
    // add parent pom declaration
    Parent parent = model.getFactory().createParent();
    parent.setGroupId("org.springframework.boot");
    parent.setArtifactId("spring-boot-starter-parent");
    parent.setVersion(BasicProjectWizardIterator.BOOTVERSION);
    parent.setRelativePath("");
    prj.setPomParent(parent);
    // add 'java.version' property deducing it from 'maven.compiler.source' if present
    final Properties propertiesElement = prj.getProperties();
    final Map<String, String> propertiesMap = propertiesElement.getProperties();
    if (propertiesMap.containsKey(PROP_COMPILER_SOURCE)) {
        propertiesElement.setProperty(PROP_JAVAVERSION, propertiesMap.get(PROP_COMPILER_SOURCE));
    } else {
        propertiesElement.setProperty(PROP_JAVAVERSION, "1.8");
    }
    // remove maven.compiler.source and maven.compiler.target properties if present
    final Element propertiesElem = propertiesElement.getPeer();
    NodeList nodes = propertiesElem.getElementsByTagName(PROP_COMPILER_SOURCE);
    if (nodes.getLength() > 0) {
        model.removeChildComponent(propertiesElement.findChildComponent((Element) nodes.item(0)));
    }
    nodes = propertiesElem.getElementsByTagName(PROP_COMPILER_TARGET);
    if (nodes.getLength() > 0) {
        model.removeChildComponent(propertiesElement.findChildComponent((Element) nodes.item(0)));
    }
    // add 'spring-boot-starter' compile dependency
    Dependency dep = model.getFactory().createDependency();
    dep.setGroupId("org.springframework.boot");
    dep.setArtifactId("spring-boot-starter");
    prj.addDependency(dep);
    // add 'spring-boot-starter-test' test dependency with exclusion of JUnit vintage engine
    dep = model.getFactory().createDependency();
    dep.setGroupId("org.springframework.boot");
    dep.setArtifactId("spring-boot-starter-test");
    dep.setScope("test");
    Exclusion exclsn = model.getFactory().createExclusion();
    exclsn.setGroupId("org.junit.vintage");
    exclsn.setArtifactId("junit-vintage-engine");
    dep.addExclusion(exclsn);
    // add 'spring-boot-maven-plugin'
    prj.addDependency(dep);
    Build build = model.getFactory().createBuild();
    Plugin plugin = model.getFactory().createPlugin();
    plugin.setGroupId("org.springframework.boot");
    plugin.setArtifactId("spring-boot-maven-plugin");
    build.addPlugin(plugin);
    prj.setBuild(build);
    // create a nbactions.xml if not already present
    final FileObject projectDirectory = Utils.getActiveProject().getProjectDirectory();
    if (projectDirectory.getFileObject("nbactions.xml") == null) {
        createNbActions(projectDirectory);
    }
    // position caret at newly added parent declaration
    return model.getAccess().findPosition(parent.getPeer());
}
 
Example 17
Source File: SpringDependenciesGenerator.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
private int addDeps(POMModel model, String bootVersion, final Set<String> selectedDeps) throws Exception {
    logger.log(Level.INFO, "Adding Spring Boot dependencies: {0}", selectedDeps.toString());
    JsonNode depsMeta = InitializrService.getInstance().getDependencies(bootVersion);
    Iterator<Map.Entry<String, JsonNode>> it = depsMeta.path("dependencies").fields();
    DependencyContainer container = model.getProject();
    // iterate on "dependencies" JSON object (fields are dependency ids)
    int newCaretPos = -1;
    while (it.hasNext()) {
        Map.Entry<String, JsonNode> entry = it.next();
        if (selectedDeps.contains(entry.getKey())) {
            JsonNode depInfo = entry.getValue();
            String groupId = depInfo.path("groupId").asText();
            String artifactId = depInfo.path("artifactId").asText();
            String scope = depInfo.path("scope").asText();
            Dependency dep = container.findDependencyById(groupId, artifactId, null);
            if (dep == null) {
                dep = model.getFactory().createDependency();
                dep.setGroupId(groupId);
                dep.setArtifactId(artifactId);
                // set scope only if not 'compile'
                if (!scope.equals("compile")) {
                    if (scope.equals("compileOnly")) {
                        dep.setOptional(Boolean.TRUE);
                    } else {
                        // scope is 'runtime' or 'test'
                        dep.setScope(scope);
                    }
                }
                // manage optional version
                if (depInfo.hasNonNull("version")) {
                    dep.setVersion(depInfo.get("version").asText());
                }
                // manage optional need of extra repository
                if (depInfo.hasNonNull("repository")) {
                    addRepository(model, depsMeta, depInfo.get("repository").asText());
                    addPluginRepository(model, depsMeta, depInfo.get("repository").asText());
                }
                // manage optional need of BOM inclusion
                if (depInfo.hasNonNull("bom")) {
                    addBom(model, depsMeta, depInfo.get("bom").asText());
                }
                container.addDependency(dep);
                logger.log(Level.FINE, "Added {0}:{1}", new Object[]{groupId, artifactId});
                newCaretPos = model.getAccess().findPosition(dep.getPeer());
            }
        }
    }
    return newCaretPos;
}