Maven:在哪里放置为tomcat-maven-plugin生成的资源



我在项目的目录中src/main/webapp有CSS和JavaScript文件。我想加入将这些资源的加入和缩小版本添加到我的 WAR 文件以及tomcat-maven-plugin拾取它的位置。

我使用 yuicompressor-maven-plugin 来创建文件并将其放${project.build.directory}/${project.build.finalName} .它非常适合 maven 包,这些资源会进入 WAR 文件,但不知何故tomcat-maven-plugin根本没有看到这些资源。我应该为它使用不同的目录吗?

我的绒球:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
                <path>/MyApp</path>
                <warDirectory>${project.build.directory}/${project.build.finalName}</warDirectory>
            </configuration>
        </plugin>
        <plugin>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <optimize>true</optimize>
                <debug>true</debug>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${basedir}/src/main/resources/META-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>META-INF</targetPath>
                        <includes>
                            <include>context.xml</include>
                        </includes>
                    </resource>
                </webResources>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.3.0</version>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <configuration>
                        <excludes>
                            <exclude>**/*</exclude>
                        </excludes>
                        <aggregations>
                            <aggregation>
                                <output>${project.build.directory}/${project.build.finalName}/js/commons-pack.js</output>
                                <includes>
                                    <include>${project.build.sourceDirectory}/../webapp/js1.js</include>
                                    <include>${project.build.sourceDirectory}/../webapp/js2.js</include>
                     ...

我应该怎么做才能mvn tomcat:run也拿起我生成的文件?

使用 warSourceDirectory

<warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>

代替tomcat-maven-plugin的配置属性(warDirectory

):
<warDirectory>${project.build.directory}/${project.build.finalName}</warDirectory>

根据 tomcat-maven-plugin 文档,warSourceDirectory 是获取 Web 资源的地方,其默认值为 ${basedir}/src/main/webapp 。 这意味着,如果您不设置该属性,则需要在 ${basedir}/src/main/webapp 下生成统一/缩小的 JavaScript 文件。

如果将warSourceDirectory设置为输出文件夹,这意味着您需要在启动 Tomcat 之前生成此文件。

或者,您也可以使用 run-war 目标而不是 run ,例如 mvn tomcat6:run-war .这将使用构建目录中的爆炸战争(以及过滤的资源)。请参阅此站点。还有一些run-war-only跳过了打包阶段。

请注意,该插件现在在 Apache 上维护(所以升级一点 :-) ) 请参阅 http://tomcat.apache.org/maven-plugin-2.0/。

即使它使用安装工作,我也不确定它是最佳解决方案(关于io和构建时间)。

tomcat 运行必须能够使用来自多个目录的资源(我不确定当前的 tomcat 嵌入式 API 是否可行)。

您能否在此处添加功能请求 https://issues.apache.org/jira/browse/MTOMCAT

谢谢

最新更新