当前位置: 首页 > 面试题库 >

如何在Maven中指定额外的源/资源文件夹,以便IDE知道它们?

唐睿
2023-03-14
问题内容

我是Maven初学者,经过反复试验,我设法为WAR发行版相对于开发WAR指定了不同的属性文件(我尝试用我能想到的最简单的方法来做,但是可以建议任何更简单的解决方案)。

因此,在开发过程中,my
database.propertieslog4j.properties来自src/main/resources,同时生成它们来自的发布WAR
src/main/resources/release

到目前为止,一切都很好。

现在的问题是:因为我与Eclipse的工作,有没有办法说,里面的POM,那src/main/resources/release是一个源文件夹也使Eclipse将列出它在Project
Explorer中的其他源文件夹下,即使其他开发商导入项目到他的IDE中(即不手动将该文件夹添加为源文件夹)?

这是我的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>

问题答案:

这是我的方法。

基本上,我声明了要在默认配置下在Eclipse中看到的所有资源文件夹(由于在配置文件处于活动状态时,将 节点附加到最终POM而不是(我认为)替换现有的);然后我告诉Maven使用属性从哪个文件夹复制资源,属性的值由活动配置文件驱动。

任何评论,当然,非常感谢!

<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初学者,经过一些反复试验,我设法为发布WAR指定了与开发WAR相关的不同属性文件(我试图以我能想到的最简单的方式进行,但可以随意建议任何更简单的解决方案)。 因此,在开发过程中,我的和来自,而产生的发行版WAR它们来自。 到目前为止,一切顺利。 问题是:既然我在使用Eclipse,那么在POM中有没有一种方法可以说src/main/resources/release也是一个源文件夹,

  • 我在一个文件夹中有一个robot资源文件,并且希望在执行Maven安装时同时包含该文件夹和robot资源文件。 项目>src>main>java... 在我的pom.xml文件中,我添加了: 当我执行Maven install时,我只看到jar中的robot资源文件,而看不到它的文件夹。我如何使它使机器人资源文件在文件夹内?我是Maven的新手,所以我希望得到任何指导。我看过这篇文章,但这篇文章没

  • Marcin Grzejszczak谈论Spring Cloud Sleuth和Zipkin 点击此处查看视频

  • 资源(Asset)代表 source 文件夹中除了文章以外的所有文件,例如图片、CSS、JS 文件等。比方说,如果你的Hexo项目中只有少量图片,那最简单的方法就是将它们放在 source/images 文件夹中。然后通过类似于 ![](/images/image.jpg) 的方法访问它们。 对于那些想要更有规律地提供图片和其他资源以及想要将他们的资源分布在各个文章上的人来说,Hexo也提供了更组

  • 我在我的pom中复制并粘贴了以下取自文档的片段。xml 但当我尝试触发部署时,会收到以下消息: 这是我的目录结构: 如何更改资源的目标文件夹?为什么maven没有解析插件中的配置指令?

  • 如何在maven pom文件中定义源文件夹顺序?我目前使用的是标准的maven目录布局,但每当我运行命令“mvn eclipse:eclipse”时,源文件夹顺序如下: src/test/java src/test/Resources src/main/java src/main/Resources 如何将其设置为: src/main/java src/main/resources src/tes