简体   繁体   English

从Java Maven构建中排除资源

[英]Exclude resources from java maven build

There seem to be many many posts about similar questions however I have been unable to find exactly what I am looking for. 关于类似问题,似乎有很多帖子,但是我一直无法找到我想要的东西。

Basically, I have a Java Application built using Maven in Eclipse. 基本上,我有一个使用Maven在Eclipse中构建的Java应用程序。 When I execute the project from withing Eclipse it works correctly as it can find all files in my resources directory. 当我从Eclipse执行项目时,它可以正常工作,因为它可以在我的资源目录中找到所有文件。 When I do a normal jar with dependencies build using maven it also works. 当我使用maven使用依赖关系构建普通jar时,它也可以工作。 However, in the final item I cannot get this to work: 但是,在最后一项中,我无法使它起作用:

I would like the resources to be excluded from the main executable jar and placed into a directory on the same level as the jar itself. 我希望将资源从主要可执行jar中排除,并放置在与jar本身相同级别的目录中。 This was a user can just make changes to the settings and execute the jar, so: 这是一个用户,您可以更改设置并执行jar,因此:

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

I have the maven-jar-plugin doing this: 我有执行此操作的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>

And I have the maven-assembly-plugin creating the resources directory with all the resource files. 我有一个maven-assembly-plugin用所有资源文件创建资源目录。

The project compiles and generates the directory structure as I want it however, the class files are unable to locate anything in the resources directory even though I specifically added them the classpath in the manifest.mf 该项目可根据需要编译并生成目录结构,但是,即使我在manifest.mf中将类路径专门添加到了类文件中,类文件也无法在资源目录中找到任何内容

This is the Manifest for details: 这是清单,以了解详细信息:

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

When Executed I receive an error on this line of code: PropertyLoader.class.getClassLoader().getResourceAsStream("settings.properties"); 执行后,我在此代码行上收到错误:PropertyLoader.class.getClassLoader()。getResourceAsStream(“ settings.properties”);

The error is: 错误是:

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)

EDIT: I am only having trouble getting java to load resources, not dependencies. 编辑:我只是有麻烦让Java来加载资源,而不是依赖项。 Those appear to load correctly. 那些似乎正确加载。

I think you are making this more difficult that it need be. 我认为您正在使这变得更加困难。 Take a look at this http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html . 看看这个http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html You should simply add all of your dependencies to the pom.xml file. 您只需将所有依赖项添加到pom.xml文件中。 This way, whenever you transport your code, maven will use the pom.xml file to rebuild the project with all of your dependencies. 这样,无论何时传输代码,maven都将使用pom.xml文件以所有依赖项重建项目。 http://mvnrepository.com/ makes it even easier. http://mvnrepository.com/使其变得更加容易。 All you have to do is search for the dependency in the repository, and copy it into your pom file. 您要做的就是在存储库中搜索依赖项,并将其复制到pom文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM