英文:
Spring Boot Layered jar: Extract layers.xml to separate dependency
问题
I'm starting to catch up on the capabilities of the repackage goal in spring-boot-maven-plugin
. It looks promising, but I need to fine-tune it a little.
我开始了解spring-boot-maven-plugin
中重新打包目标的功能。看起来很有前景,但我需要稍微调整一下。
I can easily do that by creating a layers.xml
file somewhere in my project, but the problem is that I don't only have 1 project, but rather half a dozen. All of the projects need the same kind of layering, but I don't really want to copy the same configuration for every project I want to use it on.
我可以通过在项目的某个地方创建一个layers.xml
文件来轻松完成这个任务,但问题是我不只有一个项目,而是有半打。所有这些项目都需要相同类型的分层,但我不想为每个要使用它的项目复制相同的配置。
A nice-looking solution would be to extract that configuration file into a separate jar for example and have the plugin take the config file from there, but I see no way of doing it. Is there any other solution that doesn't involve me copying the configuration file to every project I have?
一个看起来不错的解决方案是将该配置文件提取到一个单独的JAR文件中,然后让插件从那里获取配置文件,但我看不到这样做的方法。是否有其他解决方案,不需要我将配置文件复制到我拥有的每个项目中?
Unfortunately, even though the projects I have use the same parent, but are not in the same multi-module project.
不幸的是,尽管我拥有的项目使用相同的父项目,但它们不在同一个多模块项目中。
英文:
I'm starting to catch up on the capabilities of the repackage goal in spring-boot-maven-plugin
.It looks promising, but I need to fine-tune it a little.
I can easily do that by creating a layers.xml
file somewhere in my project, but the problem is that I don't only have 1 project, but rather half a dozen. All of the projects need the same kind of layering, but I don't really want to copy the same configuration for every project I want to use it on.
A nice-looking solution would be to extract that configuration file into a separate jar for example and have the plugin take the config file from there, but I see no way of doing it. Is there any other solution that doesn't involve me copying the configuration file to every project I have?
Unfortunately, even though the projects I have use the same parent, but are not in the same multi-module project.
答案1
得分: 1
我成功找到了一个解决方案。
在提出问题之前,我的spring-boot-mave-plugin
配置看起来像这样:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layers>
<enabled>true</enabled>
</layers>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<phase>package</phase>
<configuration>
<layers>
<enabled>true</enabled>
<configuration><!-- something like classpath:layers.xml --></configuration>
</layers>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>artifact</groupId>
<artifactId>with-layers.xml</artifactId>
<version>...</version>
</dependency>
</dependencies>
</plugin>
通过引入maven-dependency-plugin
,解决方案变得更加复杂,它下载前面提到的依赖项,并使用以下配置在构建文件夹中解压缩它:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-shc-build-tools</id>
<goals>
<goal>unpack</goal>
</goals>
<phase>package</phase>
<configuration>
<artifactItems>
<artifactItem>
<groupId>artifact</groupId>
<artifactId>with-layers.xml</artifactId>
<version>...</version>
<type>jar</type>
<outputDirectory>${project.build.directory}</outputDirectory>
<includes>**/layers.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
<configuration>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
然后,<configuration><!-- something like classpath:layers.xml --></configuration>
这一行变成了<configuration>${project.build.directory}/layers/layers.xml</configuration>
。
英文:
I managed to work out a solution.
Before asking the question my spring-boot-mave-plugin
config looked something like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layers>
<enabled>true</enabled>
</layers>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<phase>package</phase>
<configuration>
<layers>
<enabled>true</enabled>
<configuration><!-- something like classpath:layers.xml --></configuration>
</layers>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>artifact</groupId>
<artifactId>with-layers.xml</artifactId>
<version>...</version>
</dependency>
</dependencies>
</plugin>
The solution becomes one step more convoluted, by bringing in the maven-dependency-plugin
, which downloads the before mentioned dependency and unpacks it in the build folder with this configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-shc-build-tools</id>
<goals>
<goal>unpack</goal>
</goals>
<phase>package</phase>
<configuration>
<artifactItems>
<artifactItem>
<groupId>artifact</groupId>
<artifactId>with-layers.xml</artifactId>
<version>...</version>
<type>jar</type>
<outputDirectory>${project.build.directory}</outputDirectory>
<includes>**/layers.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
<configuration>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
In turn, the line <configuration><!-- something like classpath:layers.xml --></configuration>
becomes <configuration>${project.build.directory}/layers/layers.xml</configuration>
.
答案2
得分: 0
Spring Boot Maven Plugin 的文档指出你可以手动设置 layers.xml
的路径,所以为什么不让所有的 pom.xml
指向相同的位置呢?
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.2.RELEASE</version>
<configuration>
<layers>
<enabled>true</enabled>
<configuration>${project.basedir}/../layers.xml</configuration>
</layers>
</configuration>
</plugin>
</plugins>
</build>
</project>
/../
表示从项目目录向上一级。所以,假设你有一堆项目在同一个目录下,将 layers.xml
放在那里,它就会起作用。
另一种方法是通过将 Maven 插件声明移到所谓的父 POM中来重复使用。这是一种技术,其中一系列项目的 POM 文件的共享部分被移到一个单独的 POM 文件(父 POM)中。这里有一个示例。
英文:
The documentation for Spring Boot Maven Plugin states you can set the path to the layers.xml
manually, so why not have all pom.xml
point to the same location?
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.2.RELEASE</version>
<configuration>
<layers>
<enabled>true</enabled>
<configuration>${project.basedir}/../layers.xml</configuration>
</layers>
</configuration>
</plugin>
</plugins>
</build>
</project>
The /../
means one level higher from the project directory. So say you have a bunch of projects in one directory, put the layers.xml
there and it'll work.
Another approach could be to reuse the Maven Plugin declaration by moving it to a so-called parent POM. This is a technique where the common/shared parts of the POM files of a series of projects is moved to a single POM file (the parent POM). Here's an example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论