English 中文(简体)
马文使用其他模块的资源?
原标题:Maven use resources from other modules?
  • 时间:2012-05-24 18:36:08
  •  标签:
  • java
  • maven

我的目标是提供一个 < code> AbstrictimportClass , 以及一个特定的maven 模块中要导入的文件。 并在其他模块中使用此模块扩展此类 。

s让我们说ModuleA 包含 src/main/java/MyAbstripimportClass.java src/main/resources/Myimport.csv

I now want to use the abstract import class in ModuleB. Or rather, I will extend it, use the abstract-fileimport, and a few custom functions. Then ModuleC also uses the abstracts import and some custom functions.

问题是: 抽象类中的导入与 reader InputStream 一起进行。 当我只执行模块A时, 一切都很好 。

但当我试图通过 Maven pom 将模块包含在内, 然后扩展模块以拨号导入时, 我得到 < code> NullPointerException , 在使用阅读器的行上 。

很明显,我不能这样使用外国模块资源。

但是,我怎样才能利用这个机会呢?


更新 :

模块A:

src/main/java/path/to/MyClassA.java
src/main/resources/path/to/test.txt

abstract class MyClassA {
    public static String TESTFILE = test.txt;

    List<String> doImport(String filename) {
        InputStream fileStream = resourceClass.getResourceAsStream(filename);
        //some precessing
        return list;
    }
}

B单元:

src/main/java/path/to/MyClassB.java

class MyClassB implements MyClassA {
    List<String> list = doImport(TESTFILE);
}

If I put MyClassB in same dir as A, then everything works fine. If I build B in a own module I get NullPointer for InputStream, what means the file is not found.

最佳回答

我不认为你的问题与马文有任何关系。Class.getResourcesAsStream() 解决相对你称之为类对象的相对路径。因此,如果你在抽象的类中使用这种方法,它的每一子类都可以在不同的地方寻找资源。

例如,给三个类别:

超级 :

package com.foo;
public class Super {
    { System.out.println(getClass().getResourceAsStream("test.properties")); }
}

Sub1, a subclass of 超级 :

package com.foo.bar;
import com.foo.Super;
public class Sub1 extends Super {}

子2, 另一个子类 :

package com.foo.bar.baz;
import com.foo.Super;
public class Sub2 extends Super {}

如果您创建了超级, 它将会寻找类路径资源“ / com/ foo/ test. propertys ”, 因为“ 测试. propertys” 路径是如何解决类的 com. foo. Super 。 如果您创建了子1, 它将会在“ / com/ foo/bar/ test. propertys” 中查找, 而对于子2 实例, 它将会在“ / com/ foo/bar/ baz/ test. propertys” 中查找 。

您可能想要使用资源绝对路径而不是相对路径, 或者让子类指定相对路径。 它取决于您的设计以及您试图实现的抽象路径 。

问题回答

它并不清楚您的代码是做什么的。 您能否提供您重新读取资源的方式的样本? 如果您做了正确的话 - 从类路径的资源文件中获取 ExpleStream, 应该不会有问题 。 您可以首先检查模块A. jar 里面有您的资源文件 。

你应该检查:

  1. Module B depend on Module A in pom.xml
  2. The passed in filename parameter starts with a / , that is to say, the filename parameter is /path/to/test.txt other than path/to/test.txt

如果这两个条件是Satisfield,你应该安排工作。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签