使用依赖项的资源? [英] Use a dependency's resources?

查看:76
本文介绍了使用依赖项的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Maven项目中,有一个模块(核心),该模块具有用于其类的一些资源.在模块中运行类时,它可以获取自己的资源.一切正常.

In my Maven project there is one module (core) that has a few resources for its classes. When running classes inside the module its able to get its own resources. Everything works fine.

当其他依赖于内核的模块尝试运行该类时,发生问题的地方就是中断. Java在其中寻找资源的文件夹是此模块,而不是核心模块.因此,该类失败了.

Where stuff breaks is when another module which depends on the core tries to run that class. The folder that Java is looking for resources in is this module, not the core module. So the class fails.

简而言之:如何访问依赖项的资源?

In short: How can I access the resources of a dependency?

我已经尝试通过在Core的JAR清单Class-Path: .中进行声明来做到这一点.但是,当列出JSHookLoader.class.getClassLoader().getResources("");可用的资源时(JSHookLoader在Core中,如果有任何含义),我得到:

I've experimented with trying to do this by declaring in Core's JAR Manifest Class-Path: .. However when listing the resources available with JSHookLoader.class.getClassLoader().getResources(""); (JSHookLoader is in Core if it means anything), I get:

Resource: W:\programming\quackbot-hg\impl\target\classes
File rebel.xml

Resource: W:\programming\maven-repo\org\quackbot\core\3.5-SNAPSHOT
File core-3.5-SNAPSHOT.jar
File core-3.5-SNAPSHOT.pom
File maven-metadata-local.xml
File _maven.repositories

这当然使事情变得复杂,因为我期望JAR本身位于类路径中,而不是JAR所在的目录中

This of course complicates things as I expected the JAR itself to be in the Classpath, not the directory the JAR is in

有什么建议吗?

回到这个项目,我仍然遇到这个问题.其他指南也讨论了如何使用maven-assembly-plugin和远程资源插件,但这很麻烦,因为所有模块都必须包含Monster插件XML.

Coming back to this project I still have this issue. Other guides have talked about using maven-assembly-plugin and the remote resources plugin, but thats a lot of pain as all modules have to include the monster plugin XML.

我为什么不简化这个问题:如何向资源列表中添加依赖项JAR?

Why don't I simplify the question to this: How can I add a dependencies JAR to the resource list?

  1. core.jar的/resources文件夹下有一些资源.运行core.jar,我可以在资源列表中看到/resources.
  2. impl.jar取决于core.jar.运行/resources后,它不在资源列表中,因此会造成严重破坏.

这应该足够简单,但是我该怎么做呢?我已经花了数小时试图找出一种简单而干净的方法,但无济于事.

This should be simple enough, but how can I do it? I've spend hours trying to figure out a simple clean way to do it but to no avail.

推荐答案

经过大量搜索,我终于迷失了解决方案.

After lots of searching I finally stumbled upon a solution.

我的解决方案采用了核心模块,并使用Maven Dependency Plugin解压缩,确保排除META-INF文件夹和org文件夹(已编译类所在的位置).我之所以要排除我不想要的东西,而不是明确说明我想要的东西,是因为可以轻松添加新资源,而不必一直更新我的POM.

My solution takes the core module and unpacks it with the Maven Dependency Plugin, making sure to exclude the META-INF folder and the org folder which is where the compiled classes are. The reason I'm excluding what I don't want instead of explicitly stating what I do want is so new resources can be added easily without me having to update my POM all the time.

我之所以不使用Assembly插件或远程资源插件,是因为它们使用了专用的资源模块.我认为我不必制作一个核心模块和一个核心资源模块,而核心资源模块仅包含logback和hibernate配置文件以及一些其他内容.

The reason I'm not using the assembly plugin or the remote resources plugin is because they use dedicated resource modules. I don't think I should have to make a core module and a core-resources module, with the core-resources module only containing logback and hibernate configuration files + some other stuff.

这是我正在执行此操作的副本

Here is a copy of what I'm using to do this

    <build>
        <plugins>
            <!--Extract core's resources-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>${project.groupId}</includeGroupIds>
                            <includeArtifactIds>core</includeArtifactIds>
                            <excludeTransitive>true</excludeTransitive>
                            <overWrite>true</overWrite>
                            <outputDirectory>${project.build.directory}/core-resources</outputDirectory>
                            <excludes>org/**,META-INF/**,rebel.xml</excludes>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!--New resource locations-->
        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>${project.build.directory}/core-resources</directory>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
    </build> 

这篇关于使用依赖项的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆