【问题标题】:How exclude files/folder from Maven outputDirectory如何从 Maven outputDirectory 中排除文件/文件夹
【发布时间】:2015-09-01 09:52:43
【问题描述】:

我正在使用 Spring Boot 和 JSF 开发 maven 项目。根据this 示例,托管bean .class 文件应放在/src/webapp/WEB-INF/classes 目录中,以便在JFS 视图上显示。这是通过在 pom.xml 中添加 outputDirectory 标签来实现的:

<build>
     <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>  
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <version>${maven-compiler-plugin.version}</version>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.source.version}</source>
                <target>${java.target.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>${maven-war-plugin.version}</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

但是,我注意到除了*.class文件之外,还有src/main/resources目录的存储内容:i18n文件夹、模板文件夹、*.sql文件、application.properties等....

我的问题:如何从 /src/webapp/WEB-INF/classes 目录中排除这些不必要的文件/文件夹?

【问题讨论】:

  • 删除 outputDirectory 定义没有意义。
  • 如果我删除 outputDirectory,则后端数据不会显示在 JSF 视图上。

标签: maven


【解决方案1】:

只需将您不想要的文件添加到 maven 排除。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
   <excludes>
     <exclude>**/src/main/java/myClassesToExclude*.java</exclude>
   </excludes>
 </configuration>
</plugin>

【讨论】:

    【解决方案2】:

    在 Maven 中有多种排除文件的方法。我在这里指出一般方法。

    1. 要排除资源,您只需添加一个元素。
    <project>
      ...
      <name>My Resources Plugin Practice Project</name>
      ...
      <build>
        ...
        <resources>
          <resource>
            <directory>[your directory]</directory>
            <excludes>
              <exclude>[non-resource file #1]</exclude>
              <exclude>[non-resource file #2]</exclude>
              <exclude>[non-resource file #3]</exclude>
              ...
              <exclude>[non-resource file #n]</exclude>
            </excludes>
          </resource>
          ...
        </resources>
        ...
      </build>
      ...
    </project>
    
    1. 要包括位图、jpeg 和 gif 之外的所有内容,我们可以通过以下方式简单地排除它们:
    <project>
      ...
      <name>My Resources Plugin Practice Project</name>
      ...
      <build>
        ...
        <resources>
          <resource>
            <directory>src/my-resources</directory>
            <excludes>
              <exclude>**/*.bmp</exclude>
              <exclude>**/*.jpg</exclude>
              <exclude>**/*.jpeg</exclude>
              <exclude>**/*.gif</exclude>
            </excludes>
          </resource>
          ...
        </resources>
        ...
      </build>
      ...
    </project>
    
    1. 您也可以同时拥有和元素。例如,如果您想包含文件名中不包含“test”一词的所有文本文件。
    <project>
      ...
      <name>My Resources Plugin Practice Project</name>
      ...
      <build>
        ...
        <resources>
          <resource>
            <directory>src/my-resources</directory>
            <includes>
              <include>**/*.txt</include>
            </includes>
            <excludes>
              <exclude>**/*test*.*</exclude>
            </excludes>
          </resource>
          ...
        </resources>
        ...
      </build>
      ...
    </project>
    

    资源maven.apache.org

    【讨论】:

      【解决方案3】:

      如果您想从war文件中排除,请使用[warSourceExcludes]或[packagingExcludes]标签:

      <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.6</version>
          <configuration>         
              <warSourceExcludes>WEB-INF/abc.xml,WEB-INF/pqr.xml</warSourceExcludes>
              <packagingExcludes>WEB-INF/abc.xml,WEB-INF/pqr.xml</packagingExcludes>
          </configuration>
      </plugin>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        • 2020-08-05
        • 2017-12-28
        • 1970-01-01
        • 2015-04-05
        • 2015-12-29
        相关资源
        最近更新 更多