Adding subTag to an XML tag in an XML file of another module

Hi,

I want to add a child element to an XML tag in of a XML file in a seperate module in the same project. I've used below Code

 
final VirtualFile rootPom = VfsUtil.findRelativeFile(project.getBaseDir(), "pom.xml);
if
(rootPom != null) {
    logInfo("Found a root pom.xml in parent module(project)");

    final
String basePath = project.getBasePath();
    
File stockFile = new File(contentEntryPath);
    final
String absolutePath = stockFile.getAbsolutePath();        // fix for absolute path in windows
    final
String modulePath = absolutePath.replace(basePath + File.separator, "");

    
XmlFile pomXml = (XmlFile) psiMgr.findFile(rootPom);
    final
XmlTag rootTag = pomXml.getDocument().getRootTag();

    if
(rootTag != null && rootTag.getLocalName().equals("project")) {
        logInfo("Root pom.xml is valid and adding <module> tag");
        
logInfo("Base project path : " + basePath);
        
logInfo("ContentEntry path : " + absolutePath);
        
XmlTag modulesTag = rootTag.findFirstSubTag("modules");
        if
(modulesTag == null) {
            modulesTag = rootTag.addSubTag(rootTag.createChildTag("modules", rootTag.getNamespace(), null, false), false);
        
}
        final XmlTag moduleTag = modulesTag.createChildTag("module", modulesTag.getNamespace(), modulePath, false);
        modulesTag.addSubTag(moduleTag, false);
    
}
}


But I get an exception when executing the blue colored line and below is the stacktrace.

com.intellij.util.IncorrectOperationException: Element cannot be added
 at com.intellij.psi.impl.source.tree.CompositePsiElement.a(CompositePsiElement.java:317)
 at com.intellij.psi.impl.source.tree.CompositePsiElement.add(CompositePsiElement.java:150)
 at com.intellij.psi.impl.source.xml.XmlTagImpl.addSubTag(XmlTagImpl.java:994)
 at org.adroitlogic.idea.plugin.ide.deployment.module.DeploymentUnitModuleBuilder$1$1.run(DeploymentUnitModuleBuilder.java:282)
 at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:937)
 at org.adroitlogic.idea.plugin.ide.deployment.module.DeploymentUnitModuleBuilder$1.run(DeploymentUnitModuleBuilder.java:170)
 at com.intellij.openapi.project.DumbServiceImpl.a(DumbServiceImpl.java:269)
 at com.intellij.openapi.project.DumbServiceImpl.access$800(DumbServiceImpl.java:52)
 at com.intellij.openapi.project.DumbServiceImpl$9.run(DumbServiceImpl.java:439)
 at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:302)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
 at java.awt.EventQueue.access$300(EventQueue.java:103)
 at java.awt.EventQueue$3.run(EventQueue.java:706)
 at java.awt.EventQueue$3.run(EventQueue.java:704)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
 at com.intellij.ide.IdeEventQueue.e(IdeEventQueue.java:734)
 at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:569)
 at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:382)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)


Any idea how to overccome this issue please?

Please sign in to leave a comment.