如何打包资源,以便可以从其他Maven工件中访问它们? [英] How to package resources so that they can be accessed from other Maven artifacts?

查看:83
本文介绍了如何打包资源,以便可以从其他Maven工件中访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个系统,该系统将包含

I'm writing a system, which will consist of

  1. 专有的,封闭源代码的核心组件和
  2. 几个插件,我打算与它们的源一起分发.

目前,这些只是Maven项目,例如myapp-core(专有)和myapp-plugin1(与源代码一起分发).

For now, these are simply Maven projects, let's say myapp-core (proprietary) and myapp-plugin1 (distributed with the source code).

myapp-plugin1通过以下方式使用myapp-core. myapp-plugin1中有一个主类:

myapp-plugin1 uses myapp-core in the following way. There is a main class in myapp-plugin1:

public class DisplayHouseholdsOnMapApp {
    public static void main(final String[] aArgs) {
        CoreApp app = new CoreApp();

        try {
            app.run(new MenuBarFactory(), new GlassPaneDisplay());
        } catch (final IOException | ClassNotFoundException | SQLException exception) {
            exception.printStackTrace();
        }
    }

}

CoreAppmyapp-core项目中定义,其他所有内容(MenuBarFactoryGlassPaneDisplay)-在myapp-plugin1项目中定义.

CoreApp is defined in the myapp-core project, everything else (MenuBarFactory, GlassPaneDisplay) - in the myapp-plugin1 project.

CoreApp.run内部,发生了几件事.除其他外,它尝试从存储在myapp-core/src/main/resources文件夹中的文件中加载数据.

Inside CoreApp.run several things happen. Among other things, it tries to load data from files stored in myapp-core/src/main/resources folder.

启动myapp-plugin1时出现错误-myapp-core中定义的类无法加载该文件夹中定义的文件.

When I launch myapp-plugin1, I get errors - classes defined in myapp-core cannot load files defined in that folder.

现在我使用类似的构造读取这些数据

Now I read these data using constructs like

import com.google.common.io.Files;

final List<String> lines = Files.readLines(aFile, Charsets.UTF_8);

当我在myapp-plugin1中使用该项目时,如何在myapp-core中读取这些文件,以使其起作用?

How can I read these files in myapp-core so that it works, when I use that project in myapp-plugin1 ?

更新1:堆栈跟踪.

java.io.FileNotFoundException: src\main\resources\XXXX.csv (Das System kann den angegebenen Pfad nicht finden)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:131)
    at com.google.common.io.Files$FileByteSource.openStream(Files.java:127)
    at com.google.common.io.Files$FileByteSource.openStream(Files.java:117)
    at com.google.common.io.ByteSource$AsCharSource.openStream(ByteSource.java:404)
    at com.google.common.io.CharSource.getInput(CharSource.java:87)
    at com.google.common.io.CharSource.getInput(CharSource.java:63)
    at com.google.common.io.CharStreams.readLines(CharStreams.java:325)
    at com.google.common.io.Files.readLines(Files.java:747)
    at com.google.common.io.Files.readLines(Files.java:718)

18:19:29.896 [AWT-EventQueue-0] ERROR r.a.c.p.impl.gui.PrimCityGlassPane - 
java.io.FileNotFoundException: src\main\resources\XXX.csv (Das System kann die angegebene Datei nicht finden)
    at java.io.FileInputStream.open(Native Method) ~[na:1.8.0-ea]
    at java.io.FileInputStream.<init>(FileInputStream.java:131) ~[na:1.8.0-ea]
    at com.google.common.io.Files$FileByteSource.openStream(Files.java:127) ~[guava-15.0.jar:na]
    at com.google.common.io.Files$FileByteSource.openStream(Files.java:117) ~[guava-15.0.jar:na]
    at com.google.common.io.ByteSource$AsCharSource.openStream(ByteSource.java:404) ~[guava-15.0.jar:na]
    at com.google.common.io.CharSource.getInput(CharSource.java:87) ~[guava-15.0.jar:na]
    at com.google.common.io.CharSource.getInput(CharSource.java:63) ~[guava-15.0.jar:na]
    at com.google.common.io.CharStreams.readLines(CharStreams.java:325) ~[guava-15.0.jar:na]
    at com.google.common.io.Files.readLines(Files.java:747) ~[guava-15.0.jar:na]
    at com.google.common.io.Files.readLines(Files.java:718) ~[guava-15.0.jar:na]

更新2:

我尝试使用以下代码读取数据.

I tried to read the data using following code.

import com.google.common.io.Resources;
import com.google.common.base.Charsets;

final URL resource = getClass().getClassLoader().getResource(aFile.getPath().substring
        ("src\\main\\resources\\".length()));
final List<String> lines = Resources.readLines(resource, Charsets.UTF_8);

但是现在,当我运行myapp-plugin1时,出现以下异常.

But now, when I run myapp-plugin1 I get following exception.

Exception in thread "main" java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:192)
    at com.google.common.io.Resources$UrlByteSource.<init>(Resources.java:79)
    at com.google.common.io.Resources$UrlByteSource.<init>(Resources.java:74)
    at com.google.common.io.Resources.asByteSource(Resources.java:68)
    at com.google.common.io.Resources.asCharSource(Resources.java:113)
    at com.google.common.io.Resources.newReaderSupplier(Resources.java:104)
    at com.google.common.io.Resources.readLines(Resources.java:154)
    at com.google.common.io.Resources.readLines(Resources.java:176)

推荐答案

当然,您可以在具有依赖项的另一个工件中使用一个工件,但是您需要资源. 因此,在这里,您可以使用maven-dependency-plugin来实现以下目标:解包,请在需要时分阶段解压.您可以放置​​在outputDirectory = target/classs中的所有未打包人员 这是一个使用maven-dependency-plugin解压缩两个工件的示例:

Sure you can use one artifact in an other with a dependency but you ask for resources. So here how you can do, use the maven-dependency-plugin with the goal: unpack, take care you unpack in a phase befor you need them. All the unpacked staff you can place in outputDirectory=target/classes Here an example with unpacking of two artifacts with the maven-dependency-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org...</groupId>
                        <artifactId>...</artifactId>
                        <version>...</version>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>target/classes</outputDirectory>
                        <includes>**/*.class,**/*.xml</includes>
                    </artifactItem>
                    <artifactItem>
                        <groupId>...</groupId>
                        <artifactId>...</artifactId>
                        <version>...</version>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>target/classes</outputDirectory>
                        <includes>**/*.class</includes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

这篇关于如何打包资源,以便可以从其他Maven工件中访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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