zoukankan      html  css  js  c++  java
  • Moving property files outside jar in spring standalone application

    In one of Spring standalone project, we needed to move the property files out of the jar for easy configurability.
    I followed following steps:
    1. Move property files out of JAR and put in a directory say “target/lib”

    <plugin>

           <artifactId>maven-antrun-plugin</artifactId>
           <executions>
             <execution>
               <phase>validate</phase>
               <goals>
                 <goal>run</goal>
               </goals>
               <configuration>
                 <tasks>
            <copy todir="target/lib" overwrite="true">
                <fileset dir="src/main/resources/">
                    <include name="*.properties"/>
                    <include name="*.xml"/>
                </fileset>
            </copy>
                 </tasks>
               </configuration>
             </execution>
           </executions>
         </plugin>

    2. Exclude inclusion of files from the JAR. This will include only .hbm files in resource and any XML file in META-INF (I wanted to keep application-context.xml used by spring inside JAR)

    <resource>

        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/*.hbm.xml</include>
            <include>META-INF/*.xml</include>
        </includes>
    </resource>
     
    3. Use maven-jar plugin to include class path information in MANIFEST.MF. This one is MOST important
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                               <mainClass>
                                     com.usat.digitalportal.service.impl.BootStrap
                                </mainClass>
                </manifest>
                <manifestEntries>
                     <Class-Path>. lib</Class-Path>
            </manifestEntries>
            </archive>
        </configuration>
    </plugin>

     a. Use “classpathPrefix” to specify folder name in which all properties will be placed.
    b. Use “Class-Path” to specify the folder. “.” Indicate current folder, while “lib” specifies “lib” folder in same directory as JAR (I have used lib).
    4. Changes in spring application-context.xml
    a. Add line to look for property file in declared class path

    <context:property-placeholderlocation="classpath*:**/settings.properties, classpath*:**/usat.properties” />

    b. Add line to import resources from class path

    <importresource="classpath*:**/dao-config.xml"/> 

    This is all which is needed. Run maven target as –X clean install and it should Generate a lib folder

  • 相关阅读:
    怎样使用 Apache ab 以及 OneAPM 进行压力測试?
    opencv之haar特征+AdaBoos分类器算法流程(三)
    分区函数Partition By的与row_number()的用法以及与排序rank()的用法详解(获取分组(分区)中前几条记录)
    REST API 调用 方法
    WebApi的安全性及其解决方案
    使用Topshelf创建Windows服务
    LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count
    sqlbulkcopy 多表批量保存
    C#使用HttpWebRequest和HttpWebResponse上传文件示例
    C#模拟客户端发送数据示例
  • 原文地址:https://www.cnblogs.com/ukouryou/p/3026093.html
Copyright © 2011-2022 走看看