从java maven构建中排除资源

时间:2014-12-15 18:13:26

标签: java eclipse maven jar

似乎有很多关于类似问题的帖子,但我一直无法找到我想要的内容。

基本上,我在Eclipse中使用Maven构建了一个Java应用程序。当我使用Eclipse执行项目时,它可以正常工作,因为它可以找到我的资源目录中的所有文件。当我使用maven构建具有依赖关系的普通jar时,它也可以工作。但是,在最后的项目中,我无法使其发挥作用:

我希望将资源从主可执行jar中排除,并放在与jar本身相同级别的目录中。这是用户可以只更改设置并执行jar,所以:

|--root level
 |-Jar
 |-resources
  |-log4j.properties
  |-settings.properties

我有maven-jar-plugin这样做:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
        <index>true</index>
        <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.org.interfaces.SharepointClient.App</mainClass>
            <classpathPrefix>lib/</classpathPrefix>
        </manifest>
        <manifestEntries>
            <mode>development</mode>
            <url>http://org.com</url>
            <key>value</key>
            <Class-Path>resources/settings.properties resources/log4j.properties</Class-Path>
        </manifestEntries>
        </archive>
        <excludes>
            <exclude>settings.properties</exclude>
            <exclude>log4j.properties</exclude>
        </excludes>
    </configuration>
</plugin>

我有maven-assembly-plugin创建包含所有资源文件的资源目录。

项目根据需要编译并生成目录结构,但是,类文件无法在资源目录中找到任何内容,即使我在manifest.mf中专门添加了类路径

这是详情的清单:

Manifest-Version: 1.0
Build-Jdk: 1.7.0_17
Class-Path: resources/settings.properties resources/log4j.properties 
 lib/log4j-1.2.17.jar lib/jaxws-api-2.2.11.jar lib/jaxb-api-2.2.9.ja
 r lib/javax.xml.soap-api-1.3.5.jar lib/javax.annotation-api-1.2-b03
 .jar lib/jsr181-api-1.0-MR1.jar lib/saxon-9.1.0.8.jar lib/saxon-9.1
 .0.8-dom.jar
Created-By: Apache Maven 3.0.5
Main-Class: com.org.interfaces.SharepointClient.App
key: value
url: http://org.com
mode: development
Archiver-Version: Plexus Archiver

执行时,我在这行代码中收到错误:     PropertyLoader.class.getClassLoader()的getResourceAsStream( “settings.properties”);

错误是:

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at com.org.interfaces.SharepointClient.PropertyLoader.getProps(PropertyLoader.java:29)

编辑: 我只是让java加载资源而不是依赖项。那些似乎正确加载。

1 个答案:

答案 0 :(得分:0)

我认为你在需要的时候会变得更加困难。看一下这个http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html。您只需将所有依赖项添加到pom.xml文件即可。这样,每当您传输代码时,maven将使用pom.xml文件来重建包含所有依赖项的项目。 http://mvnrepository.com/让它变得更加轻松。您所要做的就是在存储库中搜索依赖项,并将其复制到您的pom文件中。

相关问题