开发者

Problem when importing Maven2 project in Eclipse

In my project, I have a resources dire开发者_如何学运维ctory (src/main/resources) that contains properties and XML files.

I want to filter only the properties files, but not any others kind of files (XML for example). Thus, I've set this in my pom.xml:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/*.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

This is working well when I run a Maven 2 package command, i.e. both XML and properties files are included in my final JAR, and only properties files have been filtered.

However, as I want to include this project in Eclipse, when I run the command mvn eclipse:eclipse, and import the project, then I have a problem with the source declared in my project properties.

In the "Java Build Path" option of Eclipse for my project, in tab "Source", I see the src/main/resources directory, but Eclipse also add filters which say to exclude all java files (Excluded: **/*.java) and include only properties files (Included: **/*.properties).

In the .classpath file generated, I get this line:

<classpathentry kind="src" path="src/main/resources" including="**/*.properties" excluding="**/*.java"/>

This way, the JAR built by Eclipse is not correct as all my XML files are not in the JAR.

How can I solve this problem?


Edit, regarding this page, I've added this in my pom.xml:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <sourceIncludes>
                    <sourceInclude>**/*.xml</sourceInclude>
                </sourceIncludes>
            </configuration>
        </plugin>
    </plugins>

However, the .classpath generated is not modified with the adequate information...


Edit again.

The addition in my previous edit works only for version 2.6.1+ of the Eclipse plugin, not for 2.6. So, I've tried with version 2.7. However, I don't know how to force the Eclipse plugin to not define the including attribute:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <sourceIncludes>
                    <sourceInclude>*</sourceInclude>
                </sourceIncludes>
            </configuration>
        </plugin>
    </plugins>

If I run the mvn eclipse:eclipse command, I get the following error:

Request to merge when 'filtering' is not identical. Original=resource src/main/resources: output=target/classes, include=[**/*.properties], exclude=[**/*.java], test=false, filtering=true, merging with=resource src/main/resources: output=target/classes, include=[], exclude=[**/*.properties|**/*.java], test=false, filtering=false


Well maven-eclipse-plugin is very bugy :( and we use version 2.5. The solution we found is copy resources to different folder. For regular maven bulid we use default filtering but for eclipse we have special profile. Here is maven-eclipse=plugin configuration in this profile:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources-step1</id>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <phase>initialize</phase>
            <configuration>
                <outputDirectory>${project.build.directory}/for-eclipse/resources</outputDirectory>
                <resources>
                    <resource>
                        <directory>${basedir}/src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>

        <execution>
            <id>copy-resources-step2</id>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <phase>initialize</phase>
            <configuration>
                <outputDirectory>${basedir}/src/main/resources</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.build.directory}/for-eclipse/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Of course you must modify include/exclude section for your needs.


I did some testing with the maven eclipse plugin 2.6, 2.5.1, 2.7 and 2.8-SNAPSHOT and none of them is indeed producing the expected result.

The only workaround I've been able to find is to use another directory for the resources you don't want to filter. Something like this:

  ...
  <build>
    <resources>
        <resource>
            <directory>src/main/resources1</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources2</directory>
            <filtering>false</filtering>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-eclipse-plugin</artifactId>
          <version>2.6</version>
      </plugin>
    </plugins>
  </build>

I agree that this is ugly. But at least, it produces a .classpath with the following entries:

  <classpathentry kind="src" path="src/main/resources1" including="**/*.properties" excluding="**/*.java"/>
  <classpathentry kind="src" path="src/main/resources2" including="**/*.xml" excluding="**/*.java"/>

That should allow you to deploy on Tomcat.

By the way, I wouldn't use the version 2.7 of the plugin because it's not working (see this thread). The version 2.6 may not produce the expected output but at least, it works.


Use properties to have Eclipse and Maven build into different directories. Now you can use Eclipse to quickly develop your code and use Maven to deploy it as a JAR.


I suggest that you either the mvn package goal as an external tool from Eclipse. I don't think that Eclipse can handle complex Maven workflows.

Even the m2eclipse plugin, which I use and highly recommend, has troubles with complex workflows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜