自定义文件作为依赖项

发布于 01-06 06:07 字数 219 浏览 3 评论 0原文

我有一个包含多个模块的 Maven 项目,

其中一个模块生成一个自定义的二进制文件。我需要这个文件作为另一个模块的输入。

我想做的就是获取这个文件作为依赖项,并在 ant 脚本的帮助下在其他模块中使用它。

我尝试了很多 Maven Assembly Plugin 和 dependency:copy-dependencies 插件,但没有成功

感谢您的任何建议

I have a maven project with several modules,

One of these modules produces a custom, binary file. I need this file as input in another module.

What I want to do, is to fetch this file as dependency an use it in an other module with the help of an ant-script.

I tries a lot with Maven Assembly Plugin and dependency:copy-dependencies plugin, but with no success

Thanks for any suggestions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦2025-01-13 06:07:21

我对我的一个项目有非常相似的要求。我试图在这里综合它,我希望这可以帮助您:

假设该项目的结构如下:

projectfoo (pom)
    |- module1 (your binary dependency)
    L module2 (the module that needs your dependency)   

让我们从 projectfoo pom 开始:

<groupId>com.dyan.sandbox</groupId>
<artifactId>projectfoo</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<modules>
    <module>module1</module>
    <module>module2</module>
</modules> 

简单......

现在是模块 1

<parent>
    <groupId>com.dyan.sandbox</groupId>
    <artifactId>projectfoo</artifactId>
    <version>0.0.1</version>
</parent>

<groupId>com.dyan.sandbox.projectfoo</groupId>
<artifactId>module1</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>make-your-resource</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/resources.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

:。 .. 和描述符文件 (src/main/assemble/resources.xml):

<assembly>
    <id>resources</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>.</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

我假设您之前已经以某种方式生成了二进制资源并将其存储在src/main/resources。上面的代码所做的只是创建资源的 zip 工件,这是简化其作为 module2 中的 Maven 依赖项注入的必要步骤。

因此,现在我们只需将此 zip 工件添加为模块 2 中的依赖项:

<groupId>com.dyan.sandbox.projectfoo</groupId>
<artifactId>module2</artifactId>
<version>0.0.1</version>

<dependencies>
    <dependency>
        <groupId>com.dyan.sandbox.projectfoo</groupId>
        <artifactId>module1</artifactId>
        <version>0.0.1</version>
        <classifier>resources</classifier>
        <type>zip</type>
        <scope>provided</scope>
    </dependency>
</dependencies>

...最后使用 maven-dependency-plugin 解压它,最好在 module2 的类路径中(目标/类):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-your-resource</id>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <!-- unzip the resources in compilation folder -->
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <includeArtifactIds>module1</includeArtifactIds>
                        <excludeTransitive>true</excludeTransitive>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

...就是这样!

雅尼克.

I had a very similar requirement for a project of mine. I am trying to synthesize it here, I hope this could help you :

Let's say that the project is structured as follow :

projectfoo (pom)
    |- module1 (your binary dependency)
    L module2 (the module that needs your dependency)   

Let's start with the projectfoo pom :

<groupId>com.dyan.sandbox</groupId>
<artifactId>projectfoo</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<modules>
    <module>module1</module>
    <module>module2</module>
</modules> 

Easy....

Now the module 1 :

<parent>
    <groupId>com.dyan.sandbox</groupId>
    <artifactId>projectfoo</artifactId>
    <version>0.0.1</version>
</parent>

<groupId>com.dyan.sandbox.projectfoo</groupId>
<artifactId>module1</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>make-your-resource</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/resources.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

... and the descriptor file (src/main/assembly/resources.xml) :

<assembly>
    <id>resources</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>.</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

I assume here that you have previously generated your binary resource one way or another and stored it in src/main/resources. What the code above does is just creating a zip artifact of your resource, this is a necessary step to ease its injection as a maven dependency in module2.

So, now we just have to add this zip artifact as a dependency in module 2 :

<groupId>com.dyan.sandbox.projectfoo</groupId>
<artifactId>module2</artifactId>
<version>0.0.1</version>

<dependencies>
    <dependency>
        <groupId>com.dyan.sandbox.projectfoo</groupId>
        <artifactId>module1</artifactId>
        <version>0.0.1</version>
        <classifier>resources</classifier>
        <type>zip</type>
        <scope>provided</scope>
    </dependency>
</dependencies>

... and finally unzip it with the maven-dependency-plugin, ideally in the classpath of module2 (target/classes) :

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-your-resource</id>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <!-- unzip the resources in compilation folder -->
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <includeArtifactIds>module1</includeArtifactIds>
                        <excludeTransitive>true</excludeTransitive>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

... and that's it !

Yannick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文