【问题标题】:POM How to include properties file inside a jar. The pom also creates a warPOM 如何在 jar 中包含属性文件。 pom也制造了一场战争
【发布时间】:2014-04-17 00:14:41
【问题描述】:

我开发了一个 pom 来创建一个 war 文件和一个 jar 文件。如何将属性文件插入 JAR(不是战争)?我需要插入到 JAR 中的文件位于项目中的目录 properties/its 中。您可以在 maven-resources-plugin 下面的 pom 中看到我尝试在包阶段添加属性文件以及关联的 jar 目标。然后我在 maven-jar-plugin 中再次尝试,使用 process-resources 阶段和 jar 目标。

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

【问题讨论】:

  • 您为什么要尝试从 WAR 项目构建 jar?这对我来说有味道。如果您想构建两种不同类型的对象,我建议将代码拆分为模块 books.sonatype.com/mvnex-book/reference/multimodule.html 或简单地将它们分开并将 jar 作为引入的依赖项。

标签: maven


【解决方案1】:

如何将属性文件插入 JAR(不是战争)?我需要插入到 JAR 中的文件位于项目中的目录 properties/its 中。

在打包 jar 之前,使用 maven-antrun-plugin to copy 您的文件到 maven-jar-plugin 用作 jar 的源目录的同一目录中。

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 1970-01-01
    • 2011-07-06
    • 2012-09-28
    • 2018-08-09
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 2012-05-12
    相关资源
    最近更新 更多