我对maven2和java jar项目有点问题。
这是我的项目文件系统:
MyApp
-- /src/main/java
-- my.package
-- Main.java
-- /src/main/resources
-- application.properties
Pom.xml配置为使用标准maven插件具有自定义包阶段:
<!-- copy resources from /src/main/resource to target -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- create an executable jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${project.artifactId}-${project.version}</finalName>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>my.package.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- copy dependencies jars to target/lib folder -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
使用此配置并调用mvn包,一切正常:在目标文件夹中创建一个新jar,并复制资源和依赖项。 引用文件application.properties 新文件(“application.properties”) 它是由JRE发现的。 如果我从目标文件夹手动运行jar它就可以了!
现在我的问题:我想添加exec-maven-plugin直接在eclipse中执行我的项目。 这是我在pom.xml中的新插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<mainClass>my.package.Main</mainClass>
</configuration>
</plugin>
通过这种方式执行我的类并且所有依赖项都得到满足,但找不到我的资源(特别是application.properties),因为我的程序工作目录是MyApp / target的MyApp。使用配置是没用的。
我该如何解决这个问题? 提前致谢
答案 0 :(得分:0)
有两件事情可以想到。
a)不要通过maven exec运行,而只需通过Eclipse(IE运行主类)作为独立应用程序运行。您的Eclipse项目应该被编组,以便设置正确的类路径。
b)不要使用常规文件系统访问,而是将要加载的文件放在类路径上,并使用getClass()。getClassLoader()。getResourceAsStream()来加载它。