简体   繁体   中英

using same resource location executing and building a maven2 java jar project

I've got a little problem with maven2 and java jar project.

Here's my project file system:

MyApp
  -- /src/main/java
    -- my.package
      -- Main.java
  -- /src/main/resources 
    -- application.properties

Pom.xml is configured to having a customized package phase using standard maven plugin:

<!-- 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>

Using this configuration and calling mvn package everything works fine: a new jar is created inside target folder and resources and dependencies are copied. The file application.properties is referenced through new File("application.properties") and it is found by JRE. If I run manually jar from target folder it works!

Now my problem: I would like to add exec-maven-plugin to execute my project directly inside eclipse. Here's my new plugin inside 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>

In this way my class is executed and all the dependencies are satisfied but my resources (in particular application.properties) are not found because my program working directory is MyApp insted of MyApp/target. Using configuration is useless.

How can i fix this problem?? Thanks in advance

Well two things come to mind.

a) Don't run through maven exec but simply run as a standalone application through Eclipse (IE. run the main class). Your Eclipse project should be mavenized so the correct classpath will be setup.

b) Don't use regular filesystem access, in stead put the file you want to load on the classpath and use getClass().getClassLoader().getResourceAsStream() to load it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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