将内容文件添加到Maven构件中

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

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:

&gt; pipline (folder)
&gt;&gt;&gt; file1.groovy (file)
&gt;&gt;&gt; file2.groovy (file)
&gt;&gt;&gt; file3.groovy (file)
&gt; src (folder)
&gt;&gt;&gt; ...
&gt; assembly (folder)
&gt;&gt;&gt; distribution.xml (file)
&gt; pom (file)

In the pom file:

...
 &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.4.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;descriptors&gt;
                        &lt;descriptor&gt;assembly/distribution.xml&lt;/descriptor&gt;
                    &lt;/descriptors&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;trigger-assembly&lt;/id&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                           &lt;goal&gt;single&lt;/goal&gt;
                       &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
    &lt;/build&gt;                     

The assembly/distribution.xml looes like that:

&lt;assembly xmlns=&quot;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1&quot;
          xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
          xsi:schemaLocation=&quot;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1&quot;&gt;
    &lt;id&gt;distribution&lt;/id&gt;
    &lt;formats&gt;
        &lt;format&gt;jar&lt;/format&gt;
    &lt;/formats&gt;
    &lt;fileSets&gt;
        &lt;fileSet&gt;
            &lt;directory&gt;${basedir}/pipeline&lt;/directory&gt;
            &lt;includes&gt;
                &lt;include&gt;*.groovy&lt;/include&gt;
            &lt;/includes&gt;
            &lt;excludes&gt;
                &lt;exclude&gt;file2.groovy&lt;/exclude&gt;
            &lt;/excludes&gt;
        &lt;/fileSet&gt;
    &lt;/fileSets&gt;
    &lt;files&gt;
        &lt;file&gt;
            &lt;source&gt;pipeline/file2.groovy&lt;/source&gt;
            &lt;outputDirectory&gt;&lt;/outputDirectory&gt;
            &lt;filtered&gt;true&lt;/filtered&gt;
        &lt;/file&gt;
    &lt;/files&gt;
&lt;/assembly&gt;

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 &lt;outputDirectory&gt;${basedir}/pipeline&lt;/outputDirectory&gt; 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:

&lt;outputDirectory&gt;${basedir}/target/pipeline&lt;/outputDirectory&gt;

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:

https://maven.apache.org/plugins/maven-assembly-plugin/

答案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).

huangapple
  • 本文由 发表于 2020年7月26日 20:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63100348.html
匿名

发表评论

匿名网友

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

确定