简体   繁体   English

如何从一个 Maven 项目创建两个 jars 但具有不同的资源文件

[英]how to create two jars from one Maven project but with different resources files

I have a single maven project that has multiple folders in src/main/resources, I want to generate two Jars, one include src/main/resources/folder1/all properties and the other include src/main/resources/folder2/all properties.我有一个 maven 项目,它在 src/main/resources 中有多个文件夹,我想生成两个 Jars,一个包括 src/main/resources/folder1/all 属性,另一个包括 src/main/resources/folder2/all 属性.

Is their a way to achive this?他们是实现这一目标的方法吗? If not, what is the simplest way to achive my goal?如果没有,实现我的目标的最简单方法是什么?

From Introduction to Build Profiles介绍到构建配置文件

Profiles can be activated in the Maven settings, via the section.配置文件可以在 Maven 设置中激活,通过该部分。 This section takes a list of elements, each containing a profile-id inside.本节采用一个元素列表,每个元素都包含一个 profile-id。

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

Profiles listed in the tag would be activated by default every time a project use it.每次项目使用时,默认情况下都会激活标签中列出的配置文件。 Profiles can be automatically triggered based on the detected state of the build environment.可以根据检测到的构建环境的 state 自动触发配置文件。 These triggers are specified via an section in the profile itself.这些触发器是通过配置文件本身的一个部分指定的。 Currently, this detection is limited to prefix-matching of the JDK version, the presence of a system property or the value of a system property.目前,此检测仅限于 JDK 版本的前缀匹配、系统属性的存在或系统属性的值。

This allows to create a different package content based on the target environment.这允许根据目标环境创建不同的 package 内容。

If I understood your problem correctly then you can use " Maven Assembly plugin " and " The Assembly Descriptor " in the following way: first of all, you can not use a profile if you want to build 2 jar files simultaneously.如果我正确理解您的问题,那么您可以通过以下方式使用“ Maven 程序集插件”和“程序集描述符”:首先,如果要同时构建 2 个 jar 文件,则不能使用配置文件。 So, my suggestion is to exclude your config files from your jar then use Maven Assembly plugin to create a different zip file with these folders.所以,我的建议是从你的 jar 中排除你的配置文件,然后使用 Maven 程序集插件创建一个不同的 zip 文件与这些文件夹。

for example, in your case you should have 2 file descriptors like the following:例如,在您的情况下,您应该有 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>

and folder1.xml contains: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>

for "folder2" you can do it in the same way.对于“folder2”,您可以以相同的方式进行操作。

also for exclude some config files from the jar you can use this plugin:还可以从 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>

Using maven-resources-plugin as follows:使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从java maven项目中的两个不同版本的jar加载类的两个版本? - How to load two versions of a class from two different versions of jars in a java maven project? 如何从类路径中的不同jar加载一个相同的命名资源? - How to load one of same named resources from different jars in a classpath? Maven从flink项目创建了两个jar,集群中只有80mb可以工作 - Maven creating two jars from flink project and only 80mb one works in cluster 如何从Maven项目中的资源映射中获取文件 - How to get files from resources map in Maven project 如何使用 Maven 项目中 Eclipse 中的资源创建可运行的 jar - How to create runnable jar with resources in Eclipse from Maven project 如何在 maven 中使用相同的 java 文件在 JAVA 6 和 JAVA 11 中构建两个不同的 EAR,并为两个 java 版本使用不同的 jars? - How to build two different EAR in maven for with same java files in JAVA 6 and JAVA 11 with different jars for both java version? 如何将罐子集成到Maven项目 - How to integrate jars into maven project 如何使用 Eclipse/Maven 从一个模块创建多个可执行 JAR? - How to create multiple executable JARs from one module using Eclipse/Maven? 如何从 Maven 项目中的 pom.xml 中排除已签名的 jar - How to exclude signed jars from pom.xml in Maven Project 将 jars 从 Maven 项目导入到项目类路径 - Import jars to project classpath from a maven project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM