我在maven2和java jar项目上遇到了一点小问题。
下面是我的项目文件系统:
MyApp
-- /src/main/java
-- my.package
-- Main.java
-- /src/main/resources
-- application.propertiesPom.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 package,一切正常:在目标文件夹中创建一个新的jar,并复制资源和依赖项。通过new File(" 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>通过这种方式,我的类被执行了,所有的依赖项都被满足了,但是我的资源(特别是目标)没有被找到,因为我的程序工作目录是MyApp /application.properties的MyApp实例。使用配置是无用的。
我如何解决这个问题??提前感谢
发布于 2012-11-14 20:24:44
好吧,有两件事浮现在脑海中。
a)不要通过maven exec运行,而只需通过Eclipse作为独立应用程序运行(IE。运行主类)。
b)不要使用常规的文件系统访问,而是将您想要加载的文件放在类路径上,并使用getClass().getClassLoader().getResourceAsStream()加载它。
https://stackoverflow.com/questions/13375318
复制相似问题