4

我们正在尝试从同一个 pom 文件构建两个 jars(是的,我已经阅读了这个 Sonotype 博客文章说不要),因为我们需要一个包含所有资源的一个,一个没有内部政治原因的。我们已经使用我们认为应该工作的配置配置了 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>不是列表或特定扩展名。没有喜悦。我希望我们缺少一些基本的东西。

4

3 回答 3

4

我建议您将maven-assembly-plugin用于您的消费者 jar,但是由于您决心使用 maven-jar-plugin 来完成,所以让我们修复您的构建。

这里的问题是,您将阻止资源过滤的设置与实际从 jar 中排除资源的设置混淆了(两者都使用<exclude />标签)。

以下配置(内部)在打包阶段<plugins />触发第二次调用。jar: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>

有了这两种配置,您实际上将构建两个 jar。默认包含所有资源(包括过滤的 xml 和属性文件)和没有资源的二级消费者 jar。

于 2013-07-09T20:58:08.557 回答
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>
于 2013-07-09T21:23:08.220 回答
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/*/**但不工作。现在我认为它是相对于类路径,而不是项目目录。

于 2021-07-20T08:41:29.420 回答