根据WAR中的配置文件移动文件

时间:2016-03-24 12:31:09

标签: maven

实际上在pom.xml我使用Ant插件移动文件。

....
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profileVersion>DEV</profileVersion>
            <webXmlfolder>dev</webXmlfolder>
            <mode>- Developpements</mode>
            <filterFile>src/main/filters/filter-dev.properties</filterFile>
        </properties>
    </profile>

    <profile>
        <id>preprod</id>
        <properties>
            <profileVersion>PREPROD</profileVersion>
            <webXmlfolder>preprod</webXmlfolder>
            <mode>- Pre-Production</mode>
            <filterFile>src/main/filters/filter-preprod.properties</filterFile>
        </properties>
    </profile>

    <profile>
        <id>prod</id>
        <properties>
            <profileVersion>PROD</profileVersion>
            <webXmlfolder>prod</webXmlfolder>
            <filterFile>src/main/filters/filter-prod.properties</filterFile>
        </properties>
    </profile>
</profiles>
....
    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
            <execution>
                <phase>package</phase>
                <configuration>
                    <target>
                        <move
                            file="${project.build.directory}/classes/log4j.${webXmlfolder}.properties"
                            tofile="${project.build.directory}/classes/log4j.properties"
                            overwrite="true" />

                        <move
                            file="${project.build.directory}/classes/hibernate.cfg.${webXmlfolder}.xml"
                            tofile="${project.build.directory}/classes/hibernate.cfg.xml"
                            overwrite="true" />

                        <move
                            file="${project.build.directory}/classes/com/with/paper/properties/enumerations.${webXmlfolder}.properties"
                            tofile="${project.build.directory}/classes/com/with/paper/properties/enumerations.properties"
                            overwrite="true" />

                        <move
                            file="${project.build.directory}/classes/com/with/paper/properties/MessageResources.${webXmlfolder}.properties"
                            tofile="${project.build.directory}/classes/com/with/paper/properties/MessageResources.properties"
                            overwrite="true" />

                        <move
                            file="${project.build.directory}/classes/com/with/paper/properties/Parametres.${webXmlfolder}.properties"
                            tofile="${project.build.directory}/classes/com/with/paper/properties/Parametres.properties"
                            overwrite="true" />

                        <delete>
                            <fileset dir="${project.build.directory}/classes/"
                                includes="**/hibernate.cfg.*.xml" />
                            <fileset dir="${project.build.directory}/classes/"
                                includes="**/log4j.*.properties" />
                            <fileset
                                dir="${project.build.directory}/classes/com/with/paper/properties/"
                                includes="**/enumerations.*.properties" />
                            <fileset
                                dir="${project.build.directory}/classes/com/with/paper/properties/"
                                includes="**/MessageResources.*.properties" />
                            <fileset
                                dir="${project.build.directory}/classes/com/with/paper/properties/"
                                includes="**/Parametres.*.properties" />
                        </delete>
                    </target>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

可以使用其他任何东西吗? maven-resources-plugin可以解决这个问题吗?

log4j.prepro:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE

log4j.logger.org.apache=ERROR, CONSOLE

# Start of user messages definitions

log4j.logger.org.hibernate=ERROR, LOGFILE
log4j.logger.com.with=INFO, LOGFILE
log4j.logger.org.eclipse.birt=ERROR, LOGFILE
log4j.logger.PAPERLOG=INFO, PAPERLOG
log4j.logger.INITLOG=INFO, INITLOG
log4j.logger.DECLARATIONSLOG=INFO, DECLARATIONSLOG

# PAPERLOG is set to be a File appender using a PatternLayout.
log4j.appender.PAPERLOG=org.apache.log4j.RollingFileAppender
log4j.appender.PAPERLOG.MaxFileSize=10MB
log4j.appender.PAPERLOG.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/PAPERLOG.log
log4j.appender.PAPERLOG.Append=true
log4j.appender.PAPERLOG.Threshold=DEBUG
log4j.appender.PAPERLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.PAPERLOG.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - %m%n

# INITLOG is set to be a File appender using a PatternLayout.
log4j.appender.INITLOG=org.apache.log4j.RollingFileAppender
log4j.appender.INITLOG.MaxFileSize=10MB
log4j.appender.INITLOG.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/INITLOG.log
log4j.appender.INITLOG.Append=true
log4j.appender.INITLOG.Threshold=DEBUG
log4j.appender.INITLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.INITLOG.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - %m%n

# DECLARATIONSLOG is set to be a File appender using a PatternLayout.
log4j.appender.DECLARATIONSLOG=org.apache.log4j.RollingFileAppender
log4j.appender.DECLARATIONSLOG.MaxFileSize=10MB
log4j.appender.DECLARATIONSLOG.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/DECLARATIONSLOG.log
log4j.appender.DECLARATIONSLOG.Append=true
log4j.appender.DECLARATIONSLOG.Threshold=DEBUG
log4j.appender.DECLARATIONSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.DECLARATIONSLOG.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - %m%n

# End of user messages definitions

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p %l - %m%n


# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.MaxFileSize=2MB
log4j.appender.LOGFILE.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/paper.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - [%t] %-5p %c %x - %m%n

log4j.prod:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE

log4j.logger.org.apache=ERROR, CONSOLE

# Start of user messages definitions

log4j.logger.org.hibernate=ERROR, LOGFILE
log4j.logger.com.with=WARN, LOGFILE
log4j.logger.org.eclipse.birt=ERROR, LOGFILE
log4j.logger.PAPERLOG=INFO, PAPERLOG
log4j.logger.INITLOG=INFO, INITLOG
log4j.logger.DECLARATIONSLOG=INFO, DECLARATIONSLOG

# PAPERLOG is set to be a File appender using a PatternLayout.
log4j.appender.PAPERLOG=org.apache.log4j.RollingFileAppender
log4j.appender.PAPERLOG.MaxFileSize=10MB
log4j.appender.PAPERLOG.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/PAPERLOG.log
log4j.appender.PAPERLOG.Append=true
log4j.appender.PAPERLOG.Threshold=DEBUG
log4j.appender.PAPERLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.PAPERLOG.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - %m%n

# INITLOG is set to be a File appender using a PatternLayout.
log4j.appender.INITLOG=org.apache.log4j.RollingFileAppender
log4j.appender.INITLOG.MaxFileSize=10MB
log4j.appender.INITLOG.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/INITLOG.log
log4j.appender.INITLOG.Append=true
log4j.appender.INITLOG.Threshold=DEBUG
log4j.appender.INITLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.INITLOG.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - %m%n

# DECLARATIONSLOG is set to be a File appender using a PatternLayout.
log4j.appender.DECLARATIONSLOG=org.apache.log4j.RollingFileAppender
log4j.appender.DECLARATIONSLOG.MaxFileSize=10MB
log4j.appender.DECLARATIONSLOG.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/DECLARATIONSLOG.log
log4j.appender.DECLARATIONSLOG.Append=true
log4j.appender.DECLARATIONSLOG.Threshold=DEBUG
log4j.appender.DECLARATIONSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.DECLARATIONSLOG.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - %m%n

# End of user messages definitions

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p %l - %m%n


# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.MaxFileSize=2MB
log4j.appender.LOGFILE.File=/opt/tomcat5/apache-tomcat-5.5.26/logs/paper.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-23d{dd/MM/yyyy HH:mm:ss,SSS} - [%t] %-5p %c %x - %m%n

区别在于log4j.logger.com.with=[...]

MessageResources.properties:

numero.version=Développement
numero.version.livraison.dev=@majorVersion@.@minorVersion@ développement
numero.version.livraison.preprod=@majorVersion@.@minorVersion@ pré-production
numero.version.livraison.prod=@majorVersion@.@minorVersion@
version=Version

的pom.xml:

                <resource>
                    <directory>${configFolder}</directory>
                    <includes>
                        <include>enumerations.properties</include>
                        <include>MessageResources.properties</include>
                        <include>Parametres.properties</include>
                    </includes>
                    <filtering>true</filtering>
                    <targetPath>/WEB-INF/classes/com/paper/with/properties/</targetPath>
                </resource>

2 个答案:

答案 0 :(得分:1)

在这种情况下,您正在构建WAR并希望添加依赖于当前配置文件的资源。请注意我如何解决您的问题:您不想使用maven-antrun-plugin移动和删除文件。这是XY problem

我建议您创建3个文件夹:

  • src/main/config/dev/
  • src/main/config/preprod/
  • src/main/config/prod/

其中每个文件夹都包含log4j.propertieshibernate.cfg.xmlenumerations.propertiesMessageResources.propertiesParametres.properties以用于此环境。

然后,每个配置文件部分定义要使用的正确配置文件夹,maven-war-plugin正确地将该文件夹中的文件添加为资源。您还需要启用对这些资源的过滤。

<profile>
    <id>[..]</id>
    <!-- rest of configuration unchanged -->
    <properties>
        <configFolder>src/main/config/[...]</configFolder>
    </properties>
</profile>


<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <filters>
      <filter>src/main/filters/filter.properties</filter>
      <filter>${filterFile}</filter>
    </filters>
    <webResources>
      <resource>
        <directory>${configFolder}</directory>
        <includes>
          <include>log4j.properties</include>
          <include>hibernate.cfg.xml</include>
        </includes>
        <targetPath>WEB-INF/classes</targetPath>
      </resource>
      <resource>
        <directory>${configFolder}</directory>
        <includes>
          <include>enumerations.properties</include>
          <include>MessageResources.properties</include>
          <include>Parametres.properties</include>
        </includes>
        <targetPath>WEB-INF/classes/com/with/paper/properties</targetPath>
        <filtering>true</filtering>
      </resource>
    </webResources>
  </configuration>
</plugin>

并拥有您的文件:

numero.version=Développement
numero.version.livraison.dev=${majorVersion}.${minorVersion} développement
numero.version.livraison.preprod=${majorVersion}.${minorVersion} pré-production
numero.version.livraison.prod=${majorVersion}.${minorVersion}
version=Version

答案 1 :(得分:0)

您应该使用the Assembly plugin from maven。您可以为每个配置文件提供配置。 在每个装配配置中,您可以提供输入和输出内容的名称和位置。 可以找到示例配置here