如何在Maven中指定额外的源/资源文件夹,以便IDE知道它们? [英] How to specify in Maven extra source/resource folders so IDEs are aware of them?

查看:184
本文介绍了如何在Maven中指定额外的源/资源文件夹,以便IDE知道它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Maven初学者,经过一些尝试和错误,我设法为发布WAR指定了不同的属性文件(发布WAR)(我以最简单的方式我可以想到,但是所以,在开发过程中,我的 database.properties log4j.properties 来自 src / main / resources ,同时产生版本WAR,它们来自 src /主要/资源/释放



到目前为止,这么好。



问题是:由于我正在使用Eclipse,有没有办法说,在 POM 里面, src / main / resources / release 也是一个源文件夹,所以即使当另一个开发人员在他的IDE中导入项目(即不将该文件夹作为源文件夹手动添加),Eclipse也会将它列在项目资源管理器的其他源文件夹下)?



这是我的相关部分POM:

 < project xmlns =http://maven.apache.org/POM /4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>

<个人资料>
<个人资料>
< id> release< / id>
<属性>
< war-name> ROOT< / war-name>
< / properties>

< build>
< resources><! - 替换Maven默认资源目录(这可能是通过属性实现的) - >
< resource>
< filtering> true< / filtering>
< directory> $ {basedir} / src / main / resources / release< / directory>
< includes>
< include> *< / include>
< / includes>
< / resource>
< / resources>
< / build>
< / profile>
< / profiles>

解决方案

这是我这样做的。



基本上,我在默认配置中声明了我希望在Eclipse中看到的所有资源文件夹(在每个配置文件中添加它们都不起作用,因为当配置文件处于活动状态时, < resources />节点附加到最终POM而不是(如我所想)替换现有的POM);然后我告诉Maven从哪个文件夹复制资源使用属性,其值由驱动程序的活动配置文件。



任何评论当然是非常感谢! / p>

 < project xmlns =http://maven.apache.org/POM/4.0。 0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache .ORG / XSD /行家-4.0.0.xsd> 
< modelVersion> 4.0.0< / modelVersion>
< groupId> ...< / groupId>
< artifactId> ...< / artifactId>
< version> ...< / version>
< packaging> war< / packaging>
< name> ...< / name>

<! - 我的先决条件是,在Eclipse中工作时,不需要额外的步骤
来使IDE使用正确的配置,而不是
配置 - >转换到Maven项目,所以我不喜欢在配置文件中有
默认设置,必须在Eclipse项目
配置中启用>
<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< war-name> /< / war-name>

<! - 这些解决了问题:AFAICT,每个< resource />被添加到最终的POM,
所以声明一个资源文件夹在一个配置文件中没有排除其他资源
文件夹默认(即没有配置文件活动)配置。
因此,解决方案是根据当前活动的配置文件上的
,更改每个文件夹中Maven所带来的内容。以下是默认的,无配置的
活动配置。 - >
< 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><! - 这里我声明所有资源文件夹,以便它们都将在Eclipse中显示。属性值驱动包含和排除的内容。 - >
< resource><! - 这是默认的Maven主资源目录 - >
< 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><! - 这是在WAR部署在本地独立Tomcan安装上(对网页编辑有用)时的资源目录。
< 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><! - 这是WAR部署时的资源目录 - >
< 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>
< / build>

<依赖关系>
<! - 依赖关系声明 - >
< / dependencies>

< profiles><! - 以下是配置文件。在Eclipse中工作时,没有配置文件处于活动状态,因此资源将仅从src / main / resources(根据默认属性值)进行。 - >
<个人资料>
< id> local< / id><? - 这是在WAR部署在本地独立Tomcat实例(即Eclipse外部)时)
<属性>
< war-name> ROOT< / war-name>

<! - 资源将仅从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>

<个人资料>
< id> release< / id><! - 这是在WAR部署在生产服务器上时的 - >
<属性>
< war-name> ROOT< / war-name>

<! - 资源将仅从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>


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 (i.e. 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>

解决方案

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>

这篇关于如何在Maven中指定额外的源/资源文件夹,以便IDE知道它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆