简体   繁体   中英

How to specify in Maven extra source/resource folders so IDEs are aware of them?

I'm a Maven beginner, and after some trial and error, I managed to specify different properties files for the release WAR with respect to the development WAR (I tried to do it in the simplest way I could think of, but feel free to suggest any simpler solution).

So, during development, my database.properties and log4j.properties come from src/main/resources , while producing the release WAR they come from src/main/resources/release .

So far, so good.

The question is: since I'm working with Eclipse, is there a way to say, inside the POM , that the src/main/resources/release is a source folder too, so that Eclipse will list it under the other source folders in the Project Explorer, even when another developer imports the project inside his IDE (ie without adding that folder as a source folder manually)?

This is the relevant part of my POM:

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
...
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <war-name>/</war-name>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
                ...
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            ...
        </plugin>

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            ...
        </plugin>
    </plugins>
</build>

<dependencies> ... </dependencies>

<profiles>
    <profile>
        <id>release</id>
        <properties>
            <war-name>ROOT</war-name>
        </properties>

        <build>
            <resources><!-- Replace Maven default resources directory (this could probably be achieved with a property)-->
                <resource>
                    <filtering>true</filtering>
                    <directory>${basedir}/src/main/resources/release</directory>
                    <includes>
                        <include>*</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

<build>  
   ...  
   < sourceDirectory > src/main/java </ sourceDirectory >  
   < testSourceDirectory > src/test/java </ testSourceDirectory >  
   < resources >  
       < resource >  
          < directory > src/main/resources </ directory >  
       </ resource >  
   </ resources >  
   < testResources >  
       < testResource >  
          < directory > src/test/resources </ directory >  
       </ testResource >  
   </ testResources >  
   ...  
</build>  

You can specify the profile that eclipse should run maven in...

Right click on project, select Maven, and include the profiles you can eclipse to work under.

Your config should be picked up by eclipse when you do a maven update project (ie eclipse should recognise the new source folders) once you've changed the profile.

Here's how I did it.

Basically, I declare all the resources folders I want to see in Eclipse in the default configuration (adding them in each profile didn't work since when a profile is active, the <resources /> node is appended to the final POM instead of (as I thought) replacing the existing one); then I tell Maven from which folder to copy the resources using properties, whose values are driver by the active profile.

Any comment is, of course, very much appreciated!

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <packaging>war</packaging>
    <name>...</name>

    <!-- My prerequisite was that when working in Eclipse no extra steps 
         should be required to make the IDE use the right configuration than
         Configure -> Convert to Maven Project, so I didn't like having 
         default settings in a profile that must be enabled in Eclipse project
         configuration -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <war-name>/</war-name>

        <!-- These solve the problem: AFAICT, each <resource /> is added to the final POM,
             so declaring a resources folder in a profile didn't exclude other resources 
             folders declared in the default (i.e. without profiles active) configuration.
             So, the solution is to change what Maven brings in from each folder depending
             on the profile currently active. What follows is the default, no-profile
             active configuration. -->
        <res.devel.includes>**/*</res.devel.includes>
        <res.devel.excludes></res.devel.excludes>

        <res.local.includes></res.local.includes>
        <res.local.excludes>*</res.local.excludes>

        <res.release.includes></res.release.includes>
        <res.release.excludes>*</res.release.excludes>
    </properties>

    <build>
        <resources><!-- Here I declare all the resources folders, so that they will all be shown in Eclipse. Property values drive what is included and excluded. -->
            <resource><!-- This is the default Maven main resource directory -->
                <directory>${basedir}/src/main/resources-local</directory>
                <filtering>true</filtering>
                <includes>
                    <include>${res.devel.includes}</include>
                </includes>

                <excludes>
                    <exclude>${res.devel.excludes}</exclude>
                </excludes>
            </resource>

            <resource><!-- This is the resources directory for when the WAR is deployed on a local standalone Tomcan installation (useful for web pages editing) -->
                <directory>${basedir}/src/main/resources-local</directory>
                <filtering>true</filtering>
                <includes>
                    <include>${res.local.includes}</include>
                </includes>

                <excludes>
                    <exclude>${res.local.excludes}</exclude>
                </excludes>
            </resource>

            <resource><!-- This is the resource directory for when the WAR will be deployed -->
                <directory>${basedir}/src/main/resources-release</directory>
                <filtering>true</filtering>
                <includes>
                    <include>${res.release.includes}</include>
                </includes>

                <excludes>
                    <exclude>${res.release.excludes}</exclude>
                </excludes>
            </resource>
        </resources>

        <plugins>
            <!-- Plugins configurations -->
        </plugins>
    </build>

    <dependencies>
        <!-- Dependencies declarations -->
    </dependencies>

    <profiles><!-- Here are the profiles. When working in Eclipse no profile is active, so the resources will be taken only from src/main/resources (as per default properties values). -->
        <profile>
            <id>local</id><!-- This is for when the WAR is deployed on a local standalone Tomcat instance (i.e. outside of Eclipse) -->
            <properties>
                <war-name>ROOT</war-name>

                <!-- The resources will be taken only from src/main/resources-local -->
                <res.devel.includes></res.devel.includes>
                <res.devel.excludes>*</res.devel.excludes>

                <res.local.includes>*</res.local.includes>
                <res.local.excludes></res.local.excludes>

                <res.release.includes></res.release.includes>
                <res.release.excludes>*</res.release.excludes>
            </properties>
        </profile>

        <profile>
            <id>release</id><!-- This is for when the WAR is deployed on the production server -->
            <properties>
                <war-name>ROOT</war-name>

                <!-- The resources will be taken only from src/main/resources-release -->
                <res.devel.includes></res.devel.includes>
                <res.devel.excludes>*</res.devel.excludes>

                <res.local.includes></res.local.includes>
                <res.local.excludes>*</res.local.excludes>

                <res.release.includes>*</res.release.includes>
                <res.release.excludes></res.release.excludes>
            </properties>
        </profile>
    </profiles>
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM