【问题标题】:how to create two jars from one Maven project but with different resources files如何从一个 Maven 项目创建两个 jars 但具有不同的资源文件
【发布时间】:2022-01-24 13:05:11
【问题描述】:

我有一个 maven 项目,在 src/main/resources 中有多个文件夹,我想生成两个 jar,一个包含 src/main/resources/folder1/all 属性,另一个包含 src/main/resources/folder2 /所有属性。

他们是实现这一目标的方法吗?如果没有,实现我的目标的最简单方法是什么?

【问题讨论】:

  • 感觉就像xy-problem。您能否详细说明您最终想要实现的目标?肯定有办法做到这一点,但那些可能已经需要大量的“脚本”才能从 1 个 maven 项目中创建 2 个 jar。
  • 我仍然会在一个多模块项目中进行分离,您当前的一个作为“库”模块,以及两个具有不同资源的模块。
  • 分成至少两个模块。每个罐子一个。从长远来看,任何其他方法都会给您带来更多痛苦。
  • 我必须生成 2 个具有相同 java 代码但属性文件不同的 jar,每个属性文件都与一个应用程序服务器(tomcat、websphere)相关联 必须在使用 mvn install 启动构建时生成这两个 jar命令
  • 为什么这些 jars 的配置依赖于它们部署的服务器?这对我来说似乎是糟糕的设计,我会尝试将 2 分开,就像 Joop Eggen 已经建议的那样:使用单个 jar 作为共享库并将属性放在其他地方,例如另一个依赖于应用服务器、使用它的应用程序或完全外部化它的库。最后,您很可能希望为不同的环境使用不同的属性——即使这些环境使用相同的应用服务器。

标签: java maven


【解决方案1】:

如果我正确理解了您的问题,那么您可以通过以下方式使用“Maven Assembly plugin”和“The Assembly Descriptor”: 首先,如果您想同时构建 2 个 jar 文件,则不能使用 profile。 所以,我的建议是从你的 jar 中排除你的配置文件,然后使用 Maven Assembly 插件用这些文件夹创建一个不同的 zip 文件。

例如,在您的情况下,您应该有 2 个文件描述符,如下所示:

               <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>folder1</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/java/descriptors/folder1.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

folder1.xml 包含:

<assembly>
    <id>folder1</id>
    <formats>
        <format>zip</format>
    </formats>
    <files>
        <file>
            <source>
                target/${project.artifactId}-${project.version}-yourJar.jar
            </source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/resources/folder1</directory>
            <includes>
                <include>*</include>
            </includes>
            <outputDirectory>/config</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

对于“folder2”,您可以使用相同的方式进行操作。

还可以从 jar 中排除一些配置文件,您可以使用此插件:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.3</version>
  <configuration>
    <excludes>
      <exclude>**/*.properties</exclude>
    </excludes>                    
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <mainClass>package.path.to.your.main.class.MainClass</mainClass>
      </manifest>
      <manifestEntries>
        <Class-Path>conf/</Class-Path>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

使用maven-resources-plugin如下:

  <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.3</version>
      <executions>
        <execution>
          <id>copy-resources</id>
          <phase>install</phase>
          <goals>
            <goal>copy-resources</goal>
          </goals>
          <configuration>
            <outputDirectory>${basedir}/target/conf</outputDirectory>
            <resources>
              <resource>
                <directory>src/main/resources</directory>
                <includes>
                  <include>**/*.properties</include>
                </includes>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>

【讨论】:

    【解决方案2】:

    来自Introduction to Build Profiles

    可以在 Maven 设置中通过该部分激活配置文件。这部分包含一个元素列表,每个元素都包含一个 profile-id。

    <settings>
      ...
      <activeProfiles>
        <activeProfile>profile-1</activeProfile>
      </activeProfiles>
     ...
    </settings>
    

    默认情况下,标签中列出的配置文件将在每次项目使用它时被激活。 可以根据检测到的构建环境状态自动触发配置文件。这些触发器是通过配置文件本身的一个部分指定的。目前,此检测仅限于 JDK 版本的前缀匹配、系统属性的存在或系统属性的值。

    这允许根据目标环境创建不同的包内容。

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多