views:

1377

answers:

4

In my project, I have a resources directory (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
A: 

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.

Robert Munteanu
A: 

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.

Aaron Digulla
I don't really get your point. How it will solve my problem?
romaintaz
Don't use Eclipse to build the JAR, use Maven. Maven will get it right.
Aaron Digulla
I use Maven to build the JAR. The problem is that my library is used as a J2EE module dependency for a web application. Thus, when I publish my webapp to Tomcat inside Eclipse, the JAR created by Eclipse does not include my XML files...
romaintaz
Use maven to deploy to Tomcat. Change your code so you can test everything inside of Eclipse without deploying. MockRunner is your friend.
Aaron Digulla
+1  A: 

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.

Pascal Thivent
+1  A: 

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.

cetnar