Spring Boot分层jar:将layers.xml提取到单独的依赖项中

huangapple go评论75阅读模式
英文:

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:

            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;layers&gt;
                        &lt;enabled&gt;true&lt;/enabled&gt;
                    &lt;/layers&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;repackage&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;repackage&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;configuration&gt;
                            &lt;layers&gt;
                                &lt;enabled&gt;true&lt;/enabled&gt;
                                &lt;configuration&gt;&lt;!-- something like classpath:layers.xml --&gt;&lt;/configuration&gt;
                            &lt;/layers&gt;
                            &lt;classifier&gt;exec&lt;/classifier&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;dependencies&gt;
                    &lt;dependency&gt;
                        &lt;groupId&gt;artifact&lt;/groupId&gt;
                        &lt;artifactId&gt;with-layers.xml&lt;/artifactId&gt;
                        &lt;version&gt;...&lt;/version&gt;
                    &lt;/dependency&gt;
                &lt;/dependencies&gt;
            &lt;/plugin&gt;

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:

            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.1.1&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;copy-shc-build-tools&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;unpack&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;configuration&gt;
                            &lt;artifactItems&gt;
                                &lt;artifactItem&gt;
                                    &lt;groupId&gt;artifact&lt;/groupId&gt;
                                    &lt;artifactId&gt;with-layers.xml&lt;/artifactId&gt;
                                    &lt;version&gt;...&lt;/version&gt;
                                    &lt;type&gt;jar&lt;/type&gt;
                                    &lt;outputDirectory&gt;${project.build.directory}&lt;/outputDirectory&gt;
                                    &lt;includes&gt;**/layers.xml&lt;/includes&gt;
                                &lt;/artifactItem&gt;
                            &lt;/artifactItems&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;overWriteReleases&gt;false&lt;/overWriteReleases&gt;
                    &lt;overWriteSnapshots&gt;true&lt;/overWriteSnapshots&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;

In turn, the line &lt;configuration&gt;&lt;!-- something like classpath:layers.xml --&gt;&lt;/configuration&gt; becomes &lt;configuration&gt;${project.build.directory}/layers/layers.xml&lt;/configuration&gt;.

答案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)中。这里有一个示例

Spring Boot分层jar:将layers.xml提取到单独的依赖项中

英文:

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?

&lt;project&gt;
	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
				&lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
				&lt;configuration&gt;
					&lt;layers&gt;
						&lt;enabled&gt;true&lt;/enabled&gt;
						&lt;configuration&gt;${project.basedir}/../layers.xml&lt;/configuration&gt;
					&lt;/layers&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;
&lt;/project&gt;

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

Spring Boot分层jar:将layers.xml提取到单独的依赖项中

huangapple
  • 本文由 发表于 2020年8月7日 04:51:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63291567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定