【问题标题】:Maven 3: Assembling a Jar file containing binary resourcesMaven 3:组装一个包含二进制资源的 Jar 文件
【发布时间】:2012-03-02 07:42:30
【问题描述】:

我设置了一个 Maven 项目,该项目由两个子模块、一个 Java Jar 模块和一个使用 Npanday 创建的 Windows 可执行文件组成。我的构建运行良好。

我遇到的问题是,我想创建一个包含我的 Java 库的 Jar 文件并嵌入 Exe 文件,以便我可以从库中的代码将其作为资源加载。

似乎汇编插件是要走的路,但我在配置它时遇到了一些麻烦。我什至不知道在这种情况下这是否是正确的路径。

这里有人可以指导我走向正确的道路,或者给我一个关于这样一个程序集描述符应该是什么样子的提示吗?

克里斯

【问题讨论】:

  • 是的,组装插件应该没问题。你能显示你希望 jar 文件的内容是什么吗?
  • 我会在一个主要的“答案”中回答你的问题,因为我会继续搞乱我的换行符:-)

标签: maven assemblies maven-3


【解决方案1】:

我有一个 Java 项目,目前只包含一个测试类,因为我仍处于设置构建阶段:

模块 de.cware.utils:lib-psexec-client:

  • /de/cware/utils/psexec/client/Test.java

模块 de.cware.utils:lib-psexec-service: 输出一个名为“service.exe”的文件

我希望输出看起来像客户端 jar,但还包含“service.exe”,以便我可以从客户端 jar 中的代码加载它。

模块 de.cware.utis:lib-psexec-assembly:

  • /de/cware/utils/psexec/client/Test.java
  • /service.exe

【讨论】:

  • 好吧,我实际上设法解决了我的问题:解决方案是实现一个自定义 PlexusIoResourceCollection 并将其作为程序集插件中的依赖项引用。
【解决方案2】:

好的……看来我自己找到了一个解决方案。我知道这个问题又比较特别……因为我所有的问题似乎都是:-)

解决方案是创建一个包含 PlexusIoResourceCollection 的自定义实现的 maven 模块,并从“META-INF/plexus”目录中的 components.xml 文件中引用它。

将此作为依赖项添加到我的程序集插件后,我能够将 exe 文件嵌入到我的 jar 中:-)

组件代码如下:

package npanday.plugin.archiver;

import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: cdutz
 * Date: 02.03.12
 * Time: 12:04
 */
public class PlexusIoExeResourceCollection extends PlexusIoCompressedFileResourceCollection {

    @Override
    protected String getDefaultExtension() {
        return ".exe";
    }

    @Override
    protected InputStream getInputStream(File file) throws IOException {
        // Simply return an InputStream to the resource file.
        // This will make it embed the source as a whole.
        return new FileInputStream(file);
    }

    @Override
    public String getPath() {
        // Without overriding this, the exe would be included with its full path.
        // This way it is included directly in the root of the result archive.
        return super.getFile().getName();
    }

}

这里是 META-INF/plexus/components.xml 中的配置 xml

<component-set>
    <components>
        <component>
              <role>org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection</role>
              <role-hint>exe</role-hint>
              <implementation>npanday.plugin.archiver.PlexusIoExeResourceCollection</implementation>
              <instantiation-strategy>per-lookup</instantiation-strategy>
            </component>
    </components>
</component-set>

最后是我的程序集插件中的用法:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.npanday.plugins</groupId>
                    <artifactId>maven-exe-archiver-plugin</artifactId>
                    <version>${npanday.version}</version>
                </dependency>
            </dependencies>
        </plugin>

希望它能帮到我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2020-08-20
    • 2023-03-04
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    相关资源
    最近更新 更多