当前位置: 首页 > 知识库问答 >
问题:

maven-resources-plugin+配置文件

祁建业
2023-03-14

我正在将一个现有的项目(遗留)移到Maven。其结构如下:

ParentProject
 - EARModule
   - src
   - target
   - pom.xml (ear)
 - WARModule
   - src
     - main
       - java
         - (java packages and classes)
       - resources
         - dev
           - index.jsp
         - prod
           - index.jsp
       - webapp
         - views
           - index.jsp (original)
         - WEB-INF
   - target
   - pom.xml (war)
 - pom.xml (parent)

我打算有两个不同的概况:生产和开发。在production profile中,/warmodule/src/main/resources/prod/index.jsp应该组装到views/war目录中。在开发概要文件中,/warmodule/src/main/resources/dev/index.jsp应该复制到views/war目录中。

为此,我尝试使用maven-resources-plugin,我正在war pom.xml中配置它。

<properties>
    <prodResourcesDir>${basedir}/src/main/resources/prod</prodResourcesDir>
    <devResourcesDir>${basedir}/src/main/resources/dev</devResourcesDir>
    <viewsDir>${basedir}/target/WARModule/views</viewsDir>
</properties>

<profiles>
    <profile>
        <id>development</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <id>copy_development_index.jsp</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${viewsDir}</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${devResourcesDir}</directory>
                                        <filtering>true</filtering>
                                        <includes>
                                            <include>index.jsp</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>production</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <id>copy_production_index.jsp</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${viewsDir}</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${prodResourcesDir}</directory>
                                        <includes>
                                            <include>index.jsp</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

正如您在上面看到的,我正在创建两个概要文件(生产和开发),并在每个概要文件下配置resources插件。

日志跟踪下方:

$ mvn -P production install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] ParentProject
[INFO] WebModule Maven Webapp
[INFO] EARModule
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ParentProject 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ ParentProject ---
[INFO] Installing D:\workspace\mvn_projects\ParentProject\pom.xml to C:\Users\a048148\.m2\repository\my\org\ParentProject\0.0.2-SNAPSHOT\ParentProject-0.0.2-SNAPSHOT.pom
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building WebModule Maven Webapp 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ WebModule ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 10 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ WebModule ---
[INFO] Changes detected - recompiling the module!
[INFO] Compilation messages...
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ WebModule ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspace\mvn_projects\ParentProject\WebModule\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ WebModule ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ WebModule ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-resources-plugin:2.6:copy-resources (copy_index.jsp) @ WebModule ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ WebModule ---
[INFO] Packaging webapp
[INFO] Assembling webapp [WebModule] in [D:\workspace\mvn_projects\ParentProject\WebModule\target\WebModule]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\workspace\mvn_projects\ParentProject\WebModule\src\main\webapp]
[INFO] Webapp assembled in [3122 msecs]
[INFO] Building war: D:\workspace\mvn_projects\ParentProject\WebModule\target\WebModule.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ WebModule ---
[INFO] Installing D:\workspace\mvn_projects\ParentProject\WebModule\target\WebModule.war to C:\Users\a048148\.m2\repository\my\org\WebModule\0.0.2-SNAPSHOT\WebModule-0.0.2-SNAPSHOT.war
[INFO] Installing D:\workspace\mvn_projects\ParentProject\WebModule\pom.xml to C:\Users\a048148\.m2\repository\my\org\WebModule\0.0.2-SNAPSHOT\WebModule-0.0.2-SNAPSHOT.pom
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building EARModule 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-ear-plugin:2.8:generate-application-xml (default-generate-application-xml) @ EARModule ---
[INFO] Generating application.xml
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ EARModule ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-ear-plugin:2.8:ear (default-ear) @ EARModule ---
[INFO] Copying artifact [war:com.bbh:WebModule:0.0.1-SNAPSHOT] to [WebModule-0.0.1-SNAPSHOT.war]
[INFO] Could not find manifest file: D:\workspace\mvn_projects\ParentProject\EARModule\target\ParentProject\META-INF\MANIFEST.MF - Generating one
[INFO] Building jar: D:\workspace\mvn_projects\ParentProject\EARModule\target\ParentProject.ear
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ EARModule ---
[INFO] Installing D:\workspace\mvn_projects\ParentProject\EARModule\target\ParentProject.ear to C:\Users\a048148\.m2\repository\my\org\EARModule\0.0.1-SNAPSHOT\EARModule-0.0.1-SNAPSHOT.ear
[INFO] Installing D:\workspace\mvn_projects\ParentProject\EARModule\pom.xml to C:\Users\a048148\.m2\repository\my\org\EARModule\0.0.1-SNAPSHOT\EARModule-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] ParentProject .......................................... SUCCESS [  0.229 s]
[INFO] WebModule Maven Webapp ................................ SUCCESS [ 19.470 s]
[INFO] EARModule ............................................. SUCCESS [  3.049 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.892 s
[INFO] Finished at: 2016-12-12T15:18:14+01:00
[INFO] Final Memory: 39M/247M
[INFO] ------------------------------------------------------------------------

共有1个答案

席弘图
2023-03-14

我最终发现maven-resources-plugin可以很好地处理资源,而不是web资源(JSP)。

因此,我将方法切换到maven-war-plugin过滤特性并指定目标路径。因此,我的web pom.xml中的profiles部分最终是这样的:

<profiles>
    <profile>
        <id>development</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>${devResourcesDir}</directory>
                                <filtering>true</filtering>
                                <targetPath>views</targetPath> <!-- target/WebModule/ subdirectory -->
                                <include>index.jsp</include>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>production</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>${prodResourcesDir}</directory>
                                <filtering>true</filtering>
                                <targetPath>views</targetPath> <!-- target/WebModule/ subdirectory -->
                                <include>index.jsp</include>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这告诉maven-war-plugin将index.jsp从指定的resources目录移动到target/webmodule/views目录。它都是在打包war之前由war插件完成的,因此不需要指定生命周期阶段。

 类似资料:
  • 我对Maven声纳设置有问题。我需要在src/main/java和src/main/resources中包含我的所有项目文件,以便SonarQube将显示所有文件的结果,但排除src/main/resources/static/file中的一些文件。对于测试路径,它已经在src/test/java中默认设置 但是当我用命令提示符运行我的maven-sonar时:mvn Sonar:Sonar。有这

  • 在eclipse内部,当我做maven->Update项目时,我遇到了以下问题 我尝试了这里提供的解决方案无法计算构建计划:artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3在本地存储库中不可用 并且更新了我的Maven项目,但是我仍然遇到同样的问题,即使我试图在pom.xml文件中添加依赖项,但是没有成功地解决这个

  • 我们有以下情况:我们有单元测试和集成测试。单元测试类以结束,集成测试以结束。为了在Maven build中只执行单元测试,我们使用以下配置:

  • 问题内容: 我想用我的主要Java项目及其所有依赖项创建一个jar文件。所以我在pom文件中创建了以下插件定义: 所以我执行,将所有的依赖复制到而不是复制上,效果很好。有任何想法吗? 问题答案: 正常:您配置了名为的特殊执行,但是,直接在命令行上调用目标会创建默认执行,该默认执行与您配置的执行不同。因此,不会考虑您的配置。 在Maven中,可以在2个地方配置插件:用于所有执行(在级别使用)或用于每

  • 我试图使用maven-resources-plugin使用copy-resources目标进行一些过滤,遇到了以下错误: 为了隔离问题,我创建了一个非常简单的pom.xml,从 http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html 几乎逐字复制,运行它,并得到相同的错误。 我调用它 有

  • 问题内容: 我在项目中的Maven中有一个根模块和子模块。我正在尝试使用龙目岛。我已经添加了 根pom.xml。在子模块中,我有一个带有Lombok批注的类。当我尝试建立项目时,我得到了很多 找不到标志 我试图在这里打电话给getters和setters。 我试图在根pom和子pom中使用具有相同版本(1.16.12)的lombok -maven-plugin 以及delombok,并将带注释的类