1

I developed a pom that creates a war file and a jar file. How do I insert a properties file into the JAR(not the war)? The file I need to insert into the JAR is located in the directory, properties/its, within the project. You can see in the pom below in the maven-resources-plugin where I have attempted to add the properties file in the package phase with the associated jar goal. Then I tried again in the maven-jar-plugin, using the process-resources phase and jar goal.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>xxx.xx.Lookup</groupId>
      <artifactId>lookup</artifactId>
      <packaging>war</packaging>
      <version>${env.ENVIRONMENT}</version>
      <name>elookup</name>
      <url>http://maven.apache.org</url>
      <description>Lookup</description>
      <dependencies>
        <dependency>
          <groupId>xxx.xxx.utils</groupId>
          <artifactId>Utils</artifactId>
          <version>${env.ENVIRONMENT}</version>
        </dependency>
      </dependencies>
      <parent>
         <groupId>xxx.xx.parent_project</groupId>
         <artifactId>xxxProject</artifactId>
         <version>master-version-1.0</version>
         <relativePath>../Project/pom.xml</relativePath>
      </parent>
          <profiles>
         <profile>
                 <id>development</id>
                 <activation>
                    <activeByDefault>true</activeByDefault>
                 </activation>
                 <properties> 
                     <env>${env.ENVIRONMENT}</env>
                 </properties> 
         </profile>
         <profile>
                 <id>test</id>
                 <properties> 
                     <env>${env.ENVIRONMENT}</env>
                 </properties> 
         </profile>
         <profile>
                 <id>production</id>
                 <properties> 
                     <env>${env.ENVIRONMENT}</env>
                 </properties> 
         </profile>
      </profiles>
      <build> 
         <finalName>${project.name}-${project.version}</finalName> 
         <plugins>
         <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-war-plugin</artifactId>
               <version>2.1.1</version>
               <configuration>
                     <archive>
                         <addMavenDescriptor>false</addMavenDescriptor>
                     </archive>
                     <webResources>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                   <directory>properties/its</directory>
                                   <!-- override the destination directory for this resource -->
                                   <targetPath>properties/its</targetPath>
                            </resource>
                            <resource>
                             <!-- this is relative to the pom.xml directory -->
                                   <directory>src/main/webapp/META-INF</directory>
                                   <!-- override the destination directory for this resource -->
                                   <targetPath>META-INF</targetPath>
                                   <filtering>true</filtering>
                            </resource>
                            <resource>
                             <!-- this is relative to the pom.xml directory -->
                                  <directory>src/main/webapp/WEB-INF</directory>
                                  <!-- override the destination directory for this resource -->
                                  <targetPath>WEB-INF</targetPath>
                                  <filtering>true</filtering>
                            </resource>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                   <directory>images</directory>
                                   <!-- override the destination directory for this resource -->
                                   <targetPath>images</targetPath>
                            </resource>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                   <directory>style</directory>
                                   <!-- override the destination directory for this resource -->
                                   <targetPath>style</targetPath>
                            </resource>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                   <directory>html</directory>
                                   <!-- override the destination directory for this resource -->
                                   <targetPath>.</targetPath>
                            </resource>
                     </webResources>
                     <packagingExcludes>WEB-INF/lib/stax-api-1.0.1.jar</packagingExcludes>
                </configuration>
          </plugin>
          <plugin> 
               <groupId>org.apache.maven.plugins</groupId>  
               <artifactId>maven-eclipse-plugin</artifactId>    
               <version>2.8</version>  
               <configuration>    
                     <downloadSources>true</downloadSources>    
               </configuration>     
          </plugin> 
          <plugin>    
               <groupId>org.apache.maven.plugins</groupId>    
               <artifactId>maven-resources-plugin</artifactId>    
               <version>2.4</version>   
               <configuration>    
               <!-- specify UTF-8, ISO-8859-1 or any other file encoding -->      
                    <encoding>UTF-8</encoding>
                    <executions>
                        <execution>
                            <id>make-a-jar</id>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <resources>  
                                <resource>  
                                     <directory>properties/its</directory>
                                        <includes>
                                            <include>**/*</include>
                                        </includes>
                                     <!-- <targetPath>.</targetPath>  -->   
                                </resource>   
                            </resources>      
                        </execution>    
                    </executions>    
              </configuration>    
          </plugin>
          <plugin> 
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
               <version>2.4</version>
                   <executions>
                        <execution>
                            <id>make-a-jar</id>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                            <archive>
                                  <addMavenDescriptor>false</addMavenDescriptor>
                                  <manifest>
                                        <addClasspath>true</addClasspath>
                                  </manifest>
                            </archive>
                           </configuration>
                        </execution>
                        <execution>
                            <id>jar-resources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                            <resources>  
                                <resource>
<directory>properties/its</directory>    
                                  <targetPath>.</targetPath>   
                                </resource>   
                             </resources>
                            </configuration>
                        </execution>
                         </executions>
                     </plugin>
                     <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                  <descriptor>src/main/assembly/jar.xml</descriptor>
                </configuration>
                <executions>
                    <execution>
                      <id>make-assembly</id> 
                      <phase>package</phase> 
                      <goals>
                        <goal>single</goal>
                      </goals>
                    </execution>
                </executions>
                 </plugin>      
               </plugins>      
             <resources>  
            <resource>  
                 <directory>src/main/resources</directory>    
                 <filtering>true</filtering>   
            </resource>   
         </resources>    
         <filters>    
            <filter>../../Project/workspace/src/main/filters/eddb.${env.ENVIRONMENT}.properties</filter>  
         </filters>   
      </build>
    </project>
Kevin
  • 61
  • 3
  • 1
    Why are you trying to build a jar from a WAR project? This has a smell to me. If you are wanting to build two different types of objects i suggest either Break the code out to modules http://books.sonatype.com/mvnex-book/reference/multimodule.html or just plain seperate them and have the jar as a dependency that's brought in. – ctwomey Mar 12 '14 at 17:14

1 Answers1

0

How do I insert a properties file into the JAR(not the war)? The file I need to insert into the JAR is located in the directory, properties/its, within the project.

Before package the jar use maven-antrun-plugin to copy your file into the same directory the maven-jar-plugin uses as source directory for jar-ing.

Community
  • 1
  • 1
taringamberini
  • 2,719
  • 21
  • 29