[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.technology.m2e] Problem with m2eclipse Ecore Pom model
|
I have tried to update pom.xml using m2eclipse Ecore Pom model
(pre-maven-3 branch).
I have started with the following file:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>war</packaging>
<name>Test</name>
<version>0.0.1-SNAPSHOT</version>
</project>
and wanted to get the following file:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>war</packaging>
<name>Test</name>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<outputDirectory>bin</outputDirectory>
<resources>
<resource>
<directory>/src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
artifactId>maven-war-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
The following code creates the correct file, but the file can't be
removed (Eclipse returns "The xxx/pom.xml file has unsaved changes"):
IFile pom = project.getFile(IMavenConstants.POM_FILE_NAME);
MavenModelManager modelManager =
MavenPlugin.getDefault().getMavenModelManager();
PomResourceImpl pomResource = modelManager.loadResource(pom);
Model projectDocument = (Model)pomResource.getContents().get(0);
Build build = projectDocument.getBuild();
if (build == null) {
build = PomFactory.eINSTANCE.createBuild();
}
String outputDirectory = javaProject.getOutputLocation().toString();
build.setOutputDirectory(outputDirectory);
IClasspathEntry[] entries = javaProject.getRawClasspath();
String sourceDirectory = null;
for (int i = 0; i < entries.length; i++) {
if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
sourceDirectory = entries[i].getPath().toString();
build.setSourceDirectory(sourceDirectory);
}
}
EList<Plugin> plugins = build.getPlugins();
Plugin plugin = PomFactory.eINSTANCE.createPlugin();
plugin.setGroupId("org.apache.maven.plugins");
plugin.setArtifactId("maven-war-plugin");
Configuration configuration = PomFactory.eINSTANCE.createConfiguration();
plugin.setConfiguration(configuration);
plugins.add(plugin);
EList<Resource> resources = build.getResources();
Resource resource = PomFactory.eINSTANCE.createResource();
if (sourceDirectory != null) {
resource.setDirectory(sourceDirectory);
}
EList<String> excludes = resource.getExcludes();
excludes.add("**/*.java");
resources.add(resource);
projectDocument.setBuild(build);
try {
pomResource.save(null);
} catch (IOException e) {
throw new Exception(...);
}
pomResource.unload();
I suppose pomResource.unload is obliged because it releases the WTP's
DOMModel (SSESyncResource.doUnload()).
SSESyncResource.doUnload() releases DOMModel, but doesn't release EMF
resources.
In my opinion, this method needs to be as follows:
@Override
protected void doUnload() {
super.doUnload(); // added to release EMF resources
domModel.releaseFromEdit();
}
In this case all the resources are released, the file can be removed,
but the file isn't created correctly because of a problem in the
synchronization between WTP DOMModel and EMF Ecore Model.
The created file doesn't contain the "plugins" and "resources" elements.
I believe this issue is the cause of a lot of issues in m2eclipse
(especially in the Maven Pom Editor)
A workaround for me is to use Maven Pom model
(org.apache.maven.model.Model).
Snjeza