英文:
Adding content files to a Maven artifact
问题
我使用Maven创建了一个构件,并希望在jar
文件旁边为目标使用者添加一些内容文件。(我想添加一些Jenkins脚本,但我希希望在使用者升级到构件的新版本时,这些脚本会得到更新。)
这类似于.NET NuGet,在其中您可以向使用者项目添加内容库。
根据@tashkhisi的建议,我正在尝试使用Maven Assembly Plugin
。
项目结构:
> pipeline (文件夹)
>>> file1.groovy (文件)
>>> file2.groovy (文件)
>>> file3.groovy (文件)
> src (文件夹)
>>> ...
> assembly (文件夹)
>>> distribution.xml (文件)
> pom (文件)
在pom文件中:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptors>
<descriptor>assembly/distribution.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>trigger-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
assembly/distribution.xml
内容如下:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1">
<id>distribution</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}/pipeline</directory>
<includes>
<include>*.groovy</include>
</includes>
<excludes>
<exclude>file2.groovy</exclude>
</excludes>
</fileSet>
</fileSets>
<files>
<file>
<source>pipeline/file2.groovy</source>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
我可以在目标文件夹中看到创建了两个jar
文件:
target/myLib-1.1.0-SNAPSHOT.jar
target/myLib-1.1.0-SNAPSHOT-distribution.jar
但是,当我尝试从另一个项目中使用它时,pipeline
文件夹及其内的groovy文件并未被创建...
英文:
I create an artifact with maven and I want to add some content files to the target consumer beside the jar
file. (I want to add some Jenkins scripts, but I want these scripts to get updated when the consumer upgrades into newer version of the artifact).
This is similar to .net nuget, where you can add a content library to the consumer project.
According to @tashkhisi suggestion I'm trying Maven assembly plugin
.
The project structure:
> pipline (folder)
>>> file1.groovy (file)
>>> file2.groovy (file)
>>> file3.groovy (file)
> src (folder)
>>> ...
> assembly (folder)
>>> distribution.xml (file)
> pom (file)
In the pom file:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptors>
<descriptor>assembly/distribution.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>trigger-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
The assembly/distribution.xml
looes like that:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1">
<id>distribution</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}/pipeline</directory>
<includes>
<include>*.groovy</include>
</includes>
<excludes>
<exclude>file2.groovy</exclude>
</excludes>
</fileSet>
</fileSets>
<files>
<file>
<source>pipeline/file2.groovy</source>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
I can see in the target folder that two jar
files are created:
target/myLib-1.1.0-SNAPSHOT.jar
target/myLib-1.1.0-SNAPSHOT-distribution.jar
But when I try to consume it from another project the pipeline
folder with the groovy files is not getting created...
答案1
得分: 1
在你的配置中,你说pipeline与src
文件夹是并列的,在你的pom.xml文件中,你将outputDirectory定义为<outputDirectory>${basedir}/pipeline</outputDirectory>
,这恰好就在src
文件夹的旁边(pipeline已经在那里!)所以,如果你想要将这个pipeline文件夹放在target
文件夹中的目标jar文件旁边,你应该将这个配置修改为类似这样的内容:
<outputDirectory>${basedir}/target/pipeline</outputDirectory>
顺便说一下,使用assembly插件创建一个包含部署所需所有内容的zip文件是更好的方法,详细信息请阅读以下链接:
https://maven.apache.org/plugins/maven-assembly-plugin/
英文:
In your configuration you said pipeline is beside src
and in your pom.xml you define outputDirectory as <outputDirectory>${basedir}/pipeline</outputDirectory>
which is exactly beside src
(pipeline is already there!) so if you want to put this pipeline directory beside target jar file in target
directory you should modify this configuration to something like this:
<outputDirectory>${basedir}/target/pipeline</outputDirectory>
By the way creating a zip file which contains all the thing that you need in your deployment using assembly plugin is better approach, read the following link:
答案2
得分: 0
这是不可能的。
将某物添加为依赖项不会在您的项目中添加或创建任何文件夹。
消费者需要向项目中添加逻辑,以从依赖项中提取文件并将其复制到正确的位置(可能可以使用Maven依赖插件实现)。
英文:
This is not possible.
Adding something as dependency will not add or create any folders in your project.
The consumer will need to add logic to the project to extract files from the dependency and copy them to the right place (probably possible with the Maven dependency plugin).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论