org.netbeans.modules.maven.model.Utilities Java Examples

The following examples show how to use org.netbeans.modules.maven.model.Utilities. 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: ParentVersionErrorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testSpecialRelativePath() throws Exception { // #194281
    TestFileUtils.writeFile(work, "common.xml", "<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/maven-v4_0_0.xsd'>\n" +
            "    <modelVersion>4.0.0</modelVersion>\n" +
            "    <groupId>grp</groupId>\n" +
            "    <artifactId>common</artifactId>\n" +
            "    <version>1.0</version>\n" +
            "</project>\n");
    FileObject pom = TestFileUtils.writeFile(work, "prj/pom.xml", "<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/maven-v4_0_0.xsd'>\n" +
            "    <modelVersion>4.0.0</modelVersion>\n" +
            "    <parent>\n" +
            "        <groupId>grp</groupId>\n" +
            "        <artifactId>common</artifactId>\n" +
            "        <relativePath>../common.xml</relativePath>\n" +
            "    </parent>\n" +
            "    <artifactId>prj</artifactId>\n" +
            "</project>\n");
    POMModel model = POMModelFactory.getDefault().getModel(Utilities.createModelSource(pom));
    Project prj = ProjectManager.getDefault().findProject(pom.getParent());
    assertEquals(Collections.<ErrorDescription>emptyList(), new ParentVersionError().getErrorsForDocument(model, prj));
}
 
Example #2
Source File: POMManager.java    From jeddict with Apache License 2.0 6 votes vote down vote up
public static void reload(Project project) {
    NbMavenProject mavenProject = project.getLookup().lookup(NbMavenProject.class);
    try {
        FileObject pomFileObject = toFileObject(mavenProject.getMavenProject().getFile());
        POMModel model = POMModelFactory.getDefault().getModel(Utilities.createModelSource(pomFileObject));
        Utilities.saveChanges(model);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    RP.post(() -> {
        mavenProject.triggerDependencyDownload();
        NbMavenProject.fireMavenProjectReload(project);
    });

    SwingUtilities.invokeLater(() -> NbMavenProject.fireMavenProjectReload(project));
}
 
Example #3
Source File: EAWizardIterator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates dependencies between EAR ---> Ejb module and EAR ---> Web module
 *
 * @param earDir ear module directory
 * @param ejbInfo ejb project informations
 * @param webInfo web project informations
 */
private void addEARDependencies(File earDir, ProjectInfo ejbInfo, ProjectInfo webInfo) {
    FileObject earDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(earDir));
    if (earDirFO == null) {
        return;
    }
    List<ModelOperation<POMModel>> operations = new ArrayList<>();
    if (ejbInfo != null) {
        operations.add(ArchetypeWizards.addDependencyOperation(ejbInfo, "ejb")); // NOI18N
    }
    if (webInfo != null) {
        operations.add(ArchetypeWizards.addDependencyOperation(webInfo, "war")); // NOI18N
    }

    FileObject earPom = earDirFO.getFileObject("pom.xml"); // NOI18N
    if (earPom != null) {
        Utilities.performPOMModelOperations(earPom, operations);
    }
}
 
Example #4
Source File: MavenProjectSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void storeSettingsToPom(FileObject projectFile, final String name, final String value) {
    final ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {

        @Override
        public void performOperation(POMModel model) {
            Properties props = model.getProject().getProperties();
            if (props == null) {
                props = model.getFactory().createProperties();
                model.getProject().setProperties(props);
            }
            props.setProperty(name, value);
        }
    };
    final FileObject pom = projectFile.getFileObject("pom.xml"); //NOI18N
    try {
        pom.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
            @Override
            public void run() throws IOException {
                Utilities.performPOMModelOperations(pom, Collections.singletonList(operation));
            }
        });
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example #5
Source File: MavenSchemaCompiler.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void compileSchema(final WizardDescriptor wiz) {
    final String schemaName = (String) wiz.getProperty(JAXBWizModuleConstants.SCHEMA_NAME);
    ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {
        @Override
        public void performOperation(POMModel model) {
            org.netbeans.modules.maven.model.pom.Plugin plugin = addJaxb2Plugin(model); //NOI18N
            String packageName =
                    (String)wiz.getProperty(JAXBWizModuleConstants.PACKAGE_NAME);
            if (packageName != null && packageName.trim().length() == 0) {
                packageName = null;
            }
            addJaxb2Execution(plugin, schemaName, packageName);
        }
    };
    Utilities.performPOMModelOperations(project.getProjectDirectory().getFileObject("pom.xml"),
            Collections.singletonList(operation));
}
 
Example #6
Source File: JaxWsClientNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private BindingsModel getBindingsModel(){
    String handlerBindingFile = client.getHandlerBindingFile();
    BindingsModel bindingsModel = null;

    //if there is an existing handlerBindingFile, load it
    try{
        if(handlerBindingFile != null){
            FileObject bindingsFolder = jaxWsSupport.getBindingsFolder(false);
            if(bindingsFolder != null){
                FileObject handlerBindingFO = bindingsFolder.getFileObject(handlerBindingFile);
                if(handlerBindingFO != null){
                    ModelSource ms = org.netbeans.modules.xml.retriever.catalog.Utilities.getModelSource(handlerBindingFO, true);
                    bindingsModel =  BindingsModelFactory.getDefault().getModel(ms);
                }
            }
        }
    } catch(Exception e){
        ErrorManager.getDefault().notify(e);
        return null;
    }
    return bindingsModel;
}
 
Example #7
Source File: POMManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void reload(Project project) {
    NbMavenProject mavenProject = project.getLookup().lookup(NbMavenProject.class);
    try {
        FileObject pomFileObject = toFileObject(mavenProject.getMavenProject().getFile());
        POMModel model = POMModelFactory.getDefault().getModel(Utilities.createModelSource(pomFileObject));
        Utilities.saveChanges(model);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    RP.post(() -> {
        mavenProject.triggerDependencyDownload();
    });

    SwingUtilities.invokeLater(() -> NbMavenProject.fireMavenProjectReload(project));
}
 
Example #8
Source File: MavenGroovyExtender.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public boolean activate() {
    try {
        pom.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
            @Override
            public void run() throws IOException {
                List<ModelOperation<POMModel>> operations = new ArrayList<ModelOperation<POMModel>>();
                operations.add(new AddGroovyDependency());
                operations.add(new AddMavenCompilerPlugin());
                operations.add(new AddGroovyEclipseCompiler());
                Utilities.performPOMModelOperations(pom, operations);
            }
        });
    } catch (IOException ex) {
        return false;
    }
    return true;
}
 
Example #9
Source File: MavenGroovyExtender.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isActive() {
    final Boolean[] retValue = new Boolean[1];
    retValue[0] = false;
    try {
        pom.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
            @Override
            public void run() throws IOException {
                Utilities.performPOMModelOperations(pom, Collections.singletonList(new ModelOperation<POMModel>() {

                    @Override
                    public void performOperation(POMModel model) {
                        if (ModelUtils.hasModelDependency(model, MavenConstants.GROOVY_GROUP_ID, MavenConstants.GROOVY_ARTIFACT_ID)) {
                            retValue[0] = true;
                        } else {
                            retValue[0] = false;
                        }
                    }
                }));
            }
        });
    } catch (IOException ex) {
        return retValue[0];
    }
    return retValue[0];
}
 
Example #10
Source File: RunIDEInstallationChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void defineIDE(final String netbeansInstallation) throws IOException {
    FileObject settingsXml = FileUtil.toFileObject(SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE);
    if (settingsXml == null) {
        settingsXml = FileUtil.copyFile(FileUtil.getConfigFile("Maven2Templates/settings.xml"), FileUtil.createFolder(SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getParentFile()), "settings");
    }
    Utilities.performSettingsModelOperations(settingsXml, Collections.<ModelOperation<SettingsModel>>singletonList(new ModelOperation<SettingsModel>() {
        public @Override void performOperation(SettingsModel model) {
            Profile netbeansIde = model.getSettings().findProfileById("netbeans-ide");
            if (netbeansIde != null) {
                return;
            }
            netbeansIde = model.getFactory().createProfile();
            netbeansIde.setId("netbeans-ide");
            Activation activation = model.getFactory().createActivation();
            // XXX why does the model not have this property??
            QName ACTIVE_BY_DEFAULT = SettingsQName.createQName("activeByDefault", model.getSettingsQNames().getNamespaceVersion());
            activation.setChildElementText("activeByDefault", "true", ACTIVE_BY_DEFAULT);
            netbeansIde.setActivation(activation);
            org.netbeans.modules.maven.model.settings.Properties properties = model.getFactory().createProperties();
            properties.setProperty("netbeans.installation", netbeansInstallation);
            netbeansIde.setProperties(properties);
            model.getSettings().addProfile(netbeansIde);
        }
    }));
}
 
Example #11
Source File: MavenNbModuleImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    FileObject fo = project.getProjectDirectory().getFileObject("pom.xml"); //NOI18N
    final DependencyAdder monitor = this;
    ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {
        @Override
        public void performOperation(POMModel model) {
            synchronized (monitor) {
                for (Dependency dep : toAdd) {
                    org.netbeans.modules.maven.model.pom.Dependency mdlDep =
                            ModelUtils.checkModelDependency(model, dep.getGroupId(), dep.getArtifactId(), true);
                    mdlDep.setVersion(dep.getVersion());
                    if (dep.getScope() != null) {
                        mdlDep.setScope(dep.getScope());
                    }
                }
                toAdd.clear();
            }
        }
    };
    Utilities.performPOMModelOperations(fo, Collections.singletonList(operation));
    project.getLookup().lookup(NbMavenProject.class).synchronousDependencyDownload();
}
 
Example #12
Source File: NetBeansRunParamsIDEChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Messages({
    "# {0} - property name", "# {1} - pom.xml file", 
    "NetBeansRunParamsIDEChecker.msg_confirm=<html>New version of nbm-maven-plugin is available that doesn''t require pom.xml modification to debug or profile your project.<br>Upgrade to the new version of nbm-maven-plugin and remove the netbeans.run.params.ide property?",
    "NetBeansRunParamsIDEChecker.title_confirm=Upgrade nbm-maven-plugin to newer version",
    "NetBeansRunParamsIDEChecker.upgradeButton=Upgrade nbm-maven-plugin"
})
private static boolean removeInterpolation(File pom) {
    Object upgrade = NetBeansRunParamsIDEChecker_upgradeButton();
    Confirmation dd = new Confirmation(NetBeansRunParamsIDEChecker_msg_confirm(OLD_PROPERTY, pom), NetBeansRunParamsIDEChecker_title_confirm());
    dd.setOptions(new Object[] {upgrade, NotifyDescriptor.CANCEL_OPTION} );
    Object ret = DialogDisplayer.getDefault().notify(dd);
    if (ret != upgrade) {
        return true;
    }
    if (ret == upgrade) {
        Utilities.performPOMModelOperations(FileUtil.toFileObject(pom), 
                Arrays.<ModelOperation<POMModel>>asList(new ModelOperation[] { createUpgradePluginOperation(), createRemoveIdePropertyOperation()}));
        return false;
    }
    return false;
}
 
Example #13
Source File: ParentVersionErrorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testBasicUsage() throws Exception {
    TestFileUtils.writeFile(work, "pom.xml", "<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/maven-v4_0_0.xsd'>\n" +
            "    <modelVersion>4.0.0</modelVersion>\n" +
            "    <groupId>grp</groupId>\n" +
            "    <artifactId>common</artifactId>\n" +
            "    <version>1.1</version>\n" +
            "</project>\n");
    FileObject pom = TestFileUtils.writeFile(work, "prj/pom.xml", "<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/maven-v4_0_0.xsd'>\n" +
            "    <modelVersion>4.0.0</modelVersion>\n" +
            "    <parent>\n" +
            "        <groupId>grp</groupId>\n" +
            "        <artifactId>common</artifactId>\n" +
            "        <version>1.0</version>\n" +
            "    </parent>\n" +
            "    <version>1.0</version>\n" +
            "    <artifactId>prj</artifactId>\n" +
            "</project>\n");
    POMModel model = POMModelFactory.getDefault().getModel(Utilities.createModelSource(pom));
    Project prj = ProjectManager.getDefault().findProject(pom.getParent());
    assertEquals(1, new ParentVersionError().getErrorsForDocument(model, prj).size());
}
 
Example #14
Source File: CPExtender.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean addLibrary(Library library) throws IOException {
    if ("toplink".equals(library.getName())) { //NOI18N
        //TODO would be nice if the toplink lib shipping with netbeans be the same binary
        // then we could just copy the pieces to local repo.
        //not necessary any more. toplink will be handled by default library impl..            
        // checking source doesn't work anymore, the wizard requires the level to be 1.5 up front.
        String source = PluginPropertyUtils.getPluginProperty(project, Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER, Constants.SOURCE_PARAM, "compile", "maven.compiler.source");
        if (source == null || source.matches("1[.][0-4]")) {
            Utilities.performPOMModelOperations(project.getProjectDirectory().getFileObject("pom.xml"), Collections.singletonList(new ModelOperation<POMModel>() {
                @Override public void performOperation(POMModel model) {
                    ModelUtils.setSourceLevel(model, SL_15);
                }
            }));
        }
    }
    //shall not return true, needs processing by the fallback impl as well.
    return false;
}
 
Example #15
Source File: HudsonProviderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public @Override boolean recordAssociation(Project p, final Association a) {
    FileObject pom = pom(p);
    if (pom == null) {
        return false;
    }
    Utilities.performPOMModelOperations(pom, Collections.<ModelOperation<POMModel>>singletonList(new ModelOperation<POMModel>() {
        public @Override void performOperation(POMModel model) {
            CiManagement cim;
            if (a != null) {
                cim = model.getFactory().createCiManagement();
                cim.setSystem(HUDSON_SYSTEM);
                cim.setUrl(a.toString());
            } else {
                cim = null;
            }
            model.getProject().setCiManagement(cim);
        }
    }));
    return true; // XXX pPOMMO does not rethrow exceptions or have a return value
}
 
Example #16
Source File: ContextProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void runTaskWithinContext(Lookup context, Task task) {
    JTextComponent component = context.lookup(JTextComponent.class);
    if (component != null) {
        DataObject dobj = NbEditorUtilities.getDataObject(component.getDocument());
        if (dobj != null) {
            FileObject fo = dobj.getPrimaryFile();
            ModelSource ms = Utilities.createModelSource(fo);
            if (ms.isEditable()) {
                POMModel model = POMModelFactory.getDefault().getModel(ms);
                if (model != null) {
                    Lookup newContext = new ProxyLookup(context, Lookups.fixed(model));
                    task.run(newContext);
                    return;
                }
            }
        }
    }
    task.run(context);
}
 
Example #17
Source File: SettingsContextProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void runTaskWithinContext(Lookup context, Task task) {
    JTextComponent component = context.lookup(JTextComponent.class);
    if (component != null) {
        DataObject dobj = NbEditorUtilities.getDataObject(component.getDocument());
        if (dobj != null) {
            FileObject fo = dobj.getPrimaryFile();
            ModelSource ms = Utilities.createModelSource(fo);
            SettingsModel model = SettingsModelFactory.getDefault().getModel(ms);
            if (model != null) {
                Lookup newContext = new ProxyLookup(context, Lookups.fixed(model));
                task.run(newContext);
                return;
            }
        }
    }
    task.run(context);
}
 
Example #18
Source File: OperationsImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyMoved(Project original, File originalLoc, final String newName) throws IOException {
    if (original == null) {
        //old project call..
        project.getLookup().lookup(ProjectState.class).notifyDeleted();
    } else {
        if (original.getProjectDirectory().equals(project.getProjectDirectory())) {
            // oh well, just change the name in the pom when rename is invoked.
            FileObject pomFO = project.getProjectDirectory().getFileObject("pom.xml"); //NOI18N
            ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {
                @Override
                public void performOperation(POMModel model) {
                    model.getProject().setName(newName);
                }
            };
            Utilities.performPOMModelOperations(pomFO, Collections.singletonList(operation));
            NbMavenProject.fireMavenProjectReload(project);
        }
        checkParentProject(project.getProjectDirectory(), false, newName, originalLoc.getName());
    }
}
 
Example #19
Source File: DependencyNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public @Override Action createContextAwareInstance(final Lookup context) {
    return new AbstractAction(BTN_Open_Project()) {
        public @Override void actionPerformed(ActionEvent e) {
            Set<Project> projects = new HashSet<Project>();
            for (Artifact art : context.lookupAll(Artifact.class)) {
                File f = art.getFile();
                if (f != null) {
                    Project p = FileOwnerQuery.getOwner(org.openide.util.Utilities.toURI(f));
                    if (p != null) {
                        projects.add(p);
                    }
                }
            }
            OpenProjects.getDefault().open(projects.toArray(new NbMavenProjectImpl[projects.size()]), false, true);
        }
    };
}
 
Example #20
Source File: ModelUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param pom       FileObject that represents POM
 * @param group     
 * @param artifact
 * @param version
 * @param type
 * @param scope
 * @param classifier
 * @param acceptNull accept null values to scope,type and classifier.
 *                   If true null values will remove corresponding tag.
 */
public static void addDependency(FileObject pom,
        final String group,
        final String artifact,
        final String version,
        final String type,
        final String scope,
        final String classifier, final boolean acceptNull)
{
    ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {
        private static final String BUNDLE_TYPE = "bundle"; //NOI18N
        @Override
        public void performOperation(POMModel model) {
            Dependency dep = checkModelDependency(model, group, artifact, true);
            dep.setVersion(version);
            if (acceptNull || scope != null) {
                dep.setScope(scope);
            }
            if (acceptNull || (type != null && !BUNDLE_TYPE.equals(type))) {
                dep.setType(type);
            }
            if (acceptNull || classifier != null) {
                dep.setClassifier(classifier);
            }
        }
    };
    Utilities.performPOMModelOperations(pom, Collections.singletonList(operation));
}
 
Example #21
Source File: MavenTestNGSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages("remove_junit3_when_adding_testng=Removing JUnit 3.x dependency as TestNG has transitive dependency to JUnit 4.x.")
public void configureProject(FileObject createdFile) {
    ClassPath cp = ClassPath.getClassPath(createdFile, ClassPath.COMPILE);
    FileObject ng = cp.findResource("org.testng.annotations.Test"); //NOI18N
    if (ng == null) {
        final Project p = FileOwnerQuery.getOwner(createdFile);
        FileObject pom = p.getProjectDirectory().getFileObject("pom.xml"); //NOI18N
        ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {
            public @Override
            void performOperation(POMModel model) {
                String groupID = "org.testng"; //NOI18N
                String artifactID = "testng"; //NOI18N
                if (!hasEffectiveDependency(groupID, artifactID, p.getLookup().lookup(NbMavenProject.class))) {
                    fixJUnitDependency(model, p.getLookup().lookup(NbMavenProject.class));
                    Dependency dep = ModelUtils.checkModelDependency(model, groupID, artifactID, true);
                    dep.setVersion("6.8.1"); //NOI18N
                    dep.setScope("test"); //NOI18N
                }
            }
        };
        Utilities.performPOMModelOperations(pom, Collections.singletonList(operation));
        RequestProcessor RP = new RequestProcessor("Configure TestNG project task", 1, true); //NOI18N
        RP.post(new Runnable() {

            public void run() {
                p.getLookup().lookup(NbMavenProject.class).downloadDependencyAndJavadocSource(true);
            }
        });
    }
}
 
Example #22
Source File: POMManager.java    From jeddict with Apache License 2.0 5 votes vote down vote up
@Override
public POMManager commit() {
    execute();
    if (operations.size() > 0) {
        Utilities.performPOMModelOperations(pomFileObject, operations);
    }
    pomModel.endTransaction();
    return this;
}
 
Example #23
Source File: POMManager.java    From jeddict with Apache License 2.0 5 votes vote down vote up
public POMManager(Project project, boolean readonly) {
    //target    
    this.project = project;
    mavenProject = project.getLookup().lookup(NbMavenProject.class);
    pomFileObject = toFileObject(mavenProject.getMavenProject().getFile());
    pomModel = POMModelFactory.getDefault().createFreshModel(Utilities.createModelSource(pomFileObject));
    operations = new ArrayList<>();
    if(!readonly)pomModel.startTransaction();
}
 
Example #24
Source File: RenameProjectPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void rename(Project parentProject, final String oldName, final String newName) {
    FileObject pomFO = parentProject.getProjectDirectory().getFileObject("pom.xml"); //NOI18N
    ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {
        @Override
        public void performOperation(POMModel model) {
            List<String> modules = model.getProject().getModules();
            if (modules != null && modules.contains(oldName)) {
                //delete/add module from/to parent..
                model.getProject().removeModule(oldName);
                model.getProject().addModule(newName);
            }
        }
    };
    Utilities.performPOMModelOperations(pomFO, Collections.singletonList(operation));
}
 
Example #25
Source File: OperationsImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void checkParentProject(FileObject projectDir, final boolean delete, final String newName, final String oldName) throws IOException {
    final String prjLoc = projectDir.getNameExt();
    FileObject fo = projectDir.getParent();
    Project possibleParent = ProjectManager.getDefault().findProject(fo);
    if (possibleParent != null) {
        final NbMavenProjectImpl par = possibleParent.getLookup().lookup(NbMavenProjectImpl.class);
        if (par != null) {
            FileObject pomFO = par.getProjectDirectory().getFileObject("pom.xml"); //NOI18N                
            if(pomFO != null) {
                ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {

                    @Override
                    public void performOperation(POMModel model) {
                        MavenProject prj = par.getOriginalMavenProject();
                        if ((prj.getModules() != null && prj.getModules().contains(prjLoc)) == delete) {
                            //delete/add module from/to parent..
                            if (delete) {
                                model.getProject().removeModule(prjLoc);
                            } else {
                                model.getProject().addModule(prjLoc);
                            }
                        }
                        if (newName != null && oldName != null) {
                            if (oldName.equals(model.getProject().getArtifactId())) {
                                // is this condition necessary.. why not just overwrite the artifactID always..
                                model.getProject().setArtifactId(newName);
                            }
                        }
                    }
                };
                Utilities.performPOMModelOperations(pomFO, Collections.singletonList(operation));
            } else {
                Logger.getLogger(OperationsImpl.class.getName()).log(Level.WARNING, "no pom found for a supposed project in {0}", par.getProjectDirectory());
            }
        }
    }
    
}
 
Example #26
Source File: POMManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void commit() {
    execute();
    if (operations.size() > 0) {
        Utilities.performPOMModelOperations(pomFileObject, operations);
    }
    pomModel.endTransaction();
}
 
Example #27
Source File: ActionProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Messages({                
    "# {0} - project display name", "TXT_CompilerTooOld=<html>Project {0} contains module-info.java, but modules need maven-compiler-plugin >= 3.6.<br/>Update your pom.xml?</html>"
})
private boolean checkCompilerPlugin(final String action) {
    if (action.equals(ActionProvider.COMMAND_BUILD) || 
        action.equals(ActionProvider.COMMAND_DEBUG) ||
        action.equals(ActionProvider.COMMAND_PROFILE) ||
        action.equals(ActionProvider.COMMAND_REBUILD) ||
        action.equals(ActionProvider.COMMAND_RUN) ||
        action.equals(ActionProvider.COMMAND_TEST)) 
    {
        if (!ModuleInfoUtils.checkModuleInfoAndCompilerFit(proj)) {
            if (NbPreferences.forModule(ActionProviderImpl.class).getBoolean(SHOW_COMPILER_TOO_OLD_WARNING, true)) {
                ProjectInformation info = ProjectUtils.getInformation(proj);
                WarnPanel pnl = new WarnPanel(TXT_CompilerTooOld(info.getDisplayName()));
                Object o = DialogDisplayer.getDefault().notify(new NotifyDescriptor.Confirmation(pnl, TIT_RequiresUpdateOfPOM(), NotifyDescriptor.YES_NO_OPTION));
                if (pnl.disabledWarning()) {
                    NbPreferences.forModule(ActionProviderImpl.class).putBoolean(SHOW_COMPILER_TOO_OLD_WARNING, false);
                }
                if (o == NotifyDescriptor.YES_OPTION) {
                    RequestProcessor.getDefault().post(() -> {
                        Utilities.performPOMModelOperations(
                                proj.getProjectDirectory().getFileObject("pom.xml"),
                                Collections.singletonList(new UpdateCompilerOperation()));                            
                    });
                    return false; // false means do not continue
                }
            }                
        }
    }
    return true;
}
 
Example #28
Source File: POMManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public POMManager(Project project, boolean readonly) {
    this.project = project;
    mavenProject = project.getLookup().lookup(org.netbeans.modules.maven.api.NbMavenProject.class);
    pomFileObject = toFileObject(mavenProject.getMavenProject().getFile());
    pomModel = POMModelFactory.getDefault().createFreshModel(Utilities.createModelSource(pomFileObject));
    operations = new ArrayList<>();
    if(!readonly) {
        pomModel.startTransaction();
    }
}
 
Example #29
Source File: DependencyNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * this call is slow
 *
 * @return
 */
private Project getDependencyProject() {
    if (Artifact.SCOPE_SYSTEM.equals(art.getScope())) {
        return null;
    }
    URI uri = org.openide.util.Utilities.toURI(art.getFile());
    return FileOwnerQuery.getOwner(uri);
}
 
Example #30
Source File: DependencyNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent event) {
    final Collection<? extends Artifact> artifacts = lkp.lookupAll(Artifact.class);
    if (artifacts.isEmpty()) {
        return;
    }
    Collection<? extends NbMavenProjectImpl> res = lkp.lookupAll(NbMavenProjectImpl.class);
    Set<NbMavenProjectImpl> prjs = new HashSet<NbMavenProjectImpl>(res);
    if (prjs.size() != 1) {
        return;
    }

    final NbMavenProjectImpl project = prjs.iterator().next();
    final List<Artifact> unremoved = new ArrayList<Artifact>();

    final ModelOperation<POMModel> operation = new ModelOperation<POMModel>() {
        @Override
        public void performOperation(POMModel model) {
            for (Artifact art : artifacts) {
                org.netbeans.modules.maven.model.pom.Dependency dep =
                        model.getProject().findDependencyById(art.getGroupId(), art.getArtifactId(), null);
                if (dep != null) {
                    model.getProject().removeDependency(dep);
                } else {
                    unremoved.add(art);
                }
            }
        }
    };
    RP.post(new Runnable() {
        @Override
        public void run() {
            FileObject fo = FileUtil.toFileObject(project.getPOMFile());
            Utilities.performPOMModelOperations(fo, Collections.singletonList(operation));
            if (unremoved.size() > 0) {
                StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(DependencyNode.class, "MSG_Located_In_Parent", unremoved.size()), Integer.MAX_VALUE);
            }
        }
    });
}