maven-jar-plugin排除失败

时间:2013-07-09 19:55:57

标签: maven maven-jar-plugin

我们正在尝试从同一个pom文件中构建两个罐子(是的,我已经读过this Sonotype blog post说不要)因为我们需要一个拥有所有资源而一个没有内部政治原因。我们已经使用我们认为应该工作的配置配置了maven-jar-plugin,但始终包含资源。这是我们的pom文件的相关部分:

<plugins>
    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
            <execution>
                <id>package-consumer</id>
                <phase>package</phase>
                <configuration>
                    <classifier>consumer</classifier>
                    <resources>
                        <resource>
                            <directory>src/main/resources</directory>
                            <filtering>true</filtering>
                            <excludes>
                                <exclude>**/*.bmp</exclude>
                                <exclude>**/*.jpg</exclude>
                                <exclude>**/*.jpeg</exclude>
                                <exclude>**/*.gif</exclude>
                                <exclude>**/*.xml</exclude>
                                <exclude>**/*.sql</exclude>
                                <exclude>**/*.log4j</exclude>
                                <exclude>**/*.properties</exclude>
                                <exclude>**/*.sh</exclude>
                            </excludes>
                        </resource>
                    </resources>
                </configuration>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

当我们构建时,我们可以得到OurProject.Jar和OurProject-consumer.jar,但每个jar文件中都有相同的资源。

我们尝试了<exclude>**/*</exclude><exclude>**/resources/*.*</exclude>,而不是列表或特定扩展。没有快乐。我希望我们遗漏一些基本的东西。

3 个答案:

答案 0 :(得分:4)

我建议您使用maven-assembly-plugin作为您的消费者jar,但由于您决定使用maven-jar-plugin,让我们修复您的构建。

此处的问题是,您使用being filtered的资源设置实际上从jar中排除了资源(两者都使用<exclude />标记),从而混淆了阻止来自{{3}}的资源的设置。

以下配置(在<plugins />内)会在打包阶段触发对jar:jar的第二次调用。它将从消费者罐子中排除所需的资源(有效地做你想要的):

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
      <execution>
        <id>package-consumer</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        <configuration>
          <classifier>consumer</classifier>
          <excludes>
            <exclude>**/*.bmp</exclude>
            <exclude>**/*.jpg</exclude>
            <exclude>**/*.jpeg</exclude>
            <exclude>**/*.gif</exclude>
            <exclude>**/*.xml</exclude>
            <exclude>**/*.sql</exclude>
            <exclude>**/*.log4j</exclude>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.sh</exclude>
           </excludes>
        </configuration>
      </execution>
    </executions>
  </plugin>

虽然此配置(在<resources />内)允许对xml和属性文件进行过滤(即,在进程资源阶段使用resource:resource进行属性替换);但不适用于未经修改的图像和其他二进制文件。

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
      <include>**/*.xml</include>
      <include>**/*.properties</include> 
    </includes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <excludes>
      <exclude>**/*.xml</exclude>
      <exclude>**/*.properties</exclude>  
    </excludes>
  </resource>

使用这两种配置,您将实际构建两个罐子。默认包含所有资源(包括已过滤的xml和属性文件)和没有资源的辅助使用者jar。

答案 1 :(得分:2)

我建议这样做:

 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
          <execution>
            <id>second-jar</id>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <classifier>without</classifier>
              <excludes>
                <exclude>**/*</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

答案 2 :(得分:0)

打开 Maven(mvn -X) 的调试日志,我看到 maven-jar-plugin 的条目是这样列出的:

[INFO] Building jar: /home/ddddddddd/Code/dpt/app/app/target/app-0.0.1-SNAPSHOT.jar
[DEBUG] adding directory META-INF/
[DEBUG] adding entry META-INF/MANIFEST.MF
[DEBUG] adding directory com/
[DEBUG] adding directory com/company/
[DEBUG] adding directory com/company/dpt/
[DEBUG] adding directory com/company/dpt/app/
[DEBUG] adding directory com/company/dpt/app/extensions/
[DEBUG] adding directory stubs/
[DEBUG] adding directory stubs/requests/
[DEBUG] adding directory stubs/__files/
[DEBUG] adding directory stubs/mappings/
...
[DEBUG] adding entry com/company/dpt/app/extensions/MyClass.class
[DEBUG] adding directory META-INF/maven/
[DEBUG] adding directory META-INF/maven/com.company.dpt/
[DEBUG] adding directory META-INF/maven/com.company.dpt/app/
[DEBUG] adding entry META-INF/maven/com.company.dpt/app/pom.xml
[DEBUG] adding entry META-INF/maven/com.company.dpt/app/pom.properties

我想排除src/resources/stubs,所以我这样添加:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>stubs</exclude>
                        <exclude>stubs/*/**</exclude>
                    </excludes>
                </configuration>
            </plugin>

它有效!

之前我放了 src/resources/stubs/*/** 但不起作用。现在我认为它是相对于类路径,而不是项目目录。