webapp资源成为目标

时间:2017-04-19 13:27:30

标签: java maven maven-war-plugin

Web资源未被复制到target / classes文件夹,因此不会被复制到target / $ {finalName} / WEB-INF / classes文件夹中。

目前mvn clean install的输出:

target/classes/(src/main/java)

然而我也想要:

target/classes/(src/main/webapp)

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>
  <groupId>StrategicTestArchitecture</groupId>
  <artifactId>STATestAPI</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
  <!--        <configuration>
            <warSourceDirectory>src/main/webapp</warSourceDirectory>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <webResouces>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>WEB-INF/classes</targetPath>
            </resource>
            <resource>
                <directory>${baseDir}/src/main/webapp</directory>
                <targetPath>target/${finalName}/WEB-INF</targetPath>
            </resource>
          </webResouces>
        </configuration> -->
      </plugin>
    </plugins>
    <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>Spring.properties</include>
                </includes>
            </resource>
    </resources>
  </build>
  <profiles>
        <profile>
            <id>staging</id>
            <activation>
                <property>
                    <name>environment</name>
                    <value>staging</value>
                </property>
            </activation>
            <properties>
                <hibernate.dialect>org.hibernate.dialect.Oracle10gDialect</hibernate.dialect>
                <hibernate.show_sql>true</hibernate.show_sql>
                <hibernate.url>myoracleurl</hibernate.url>
                <DriverClassName>oracle.jdbc.driver.OracleDriver</DriverClassName>
            </properties>
        </profile>
        <profile>
            <id>alpha</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <hibernate.dialect>org.hibernate.dialect.Oracle10gDialect</hibernate.dialect>
                <hibernate.show_sql>true</hibernate.show_sql>
                <hibernate.url>myoracleurl</hibernate.url>
                <DriverClassName>oracle.jdbc.driver.OracleDriver</DriverClassName>
            </properties>
        </profile>
    </profiles>
  <properties>
        <!-- Generic properties -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>

        <!-- SPRING & HIBERNATE / JPA -->
        <spring.version>4.3.5.RELEASE</spring.version>
        <hibernate.version>5.2.5.Final</hibernate.version>

  </properties>
  <dependencies>
    <dependency>
        <groupId>StrategicTestArchitecture</groupId>
        <artifactId>Commons</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

有人可以解释为什么这些文件没有被复制到输出目录中吗?

1 个答案:

答案 0 :(得分:1)

正如Steve C所提到的,Web资源不需要在类路径上,因此Web配置可以留空。这会将src/main/webapp目录中的所有内容复制到war文件的根目录中。

在我的情况下,我们有一个名为资源的文件夹位于src/main/webapp/resources,其中包含我们的html / css / js

对于spring配置中的引用,registryhandler可以这样修改:

public void addResourceHandlers(ResourceHandlerRegistry registry){
    registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
}