如何从Maven outputDirectory中排除文件/文件夹

时间:2015-09-01 09:52:43

标签: maven

我正在使用Spring启动和JSF开发maven项目。根据{{​​3}}示例,托管bean .class文件应放在/ src / webapp / WEB-INF / classes目录中,以便在JFS视图中显示。这是通过将outputDirectory标记添加到pom.xml:

来实现的
<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目录中排除这些不必要的文件/文件夹?

3 个答案:

答案 0 :(得分:0)

只需将您不想要的文件添加到maven exlude中。

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

答案 1 :(得分:0)

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

  1. 要排除资源,您只需添加元素。
  2. <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之外的所有内容,我们只需将它们排除在:
    2. <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. 你也可以同时拥有和元素。例如,如果要包含所有不包含单词&#34; test&#34;的文本文件。在他们的文件名中。
      2. <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

答案 2 :(得分:0)

如果要从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>