Child pages
  • maven resource plugin & filtering

개요

The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources. The difference is that the main resources are the resources associated to the main source code while the test resources are associated to the test source code.

Thus, this allows the separation of resources for the main source code and its unit tests.

Starting with version 2.3 this plugin uses the Maven Filtering shared component for filtering resources.

 

Goals

The Resources Plugin copies files specified by Resource elements, to an output directory. The three variations below only differ in how the resource and output directory elements are specified or defaulted. The Resources Plugin has three goals:

  • resources:resources copies the resources for the main source code to the main output directory.

    This goal usually executes automatically, because it is bound by default to the process-resources life-cycle phase. It always uses the project.build.resources element to specify the resources, and by default uses the project.build.outputDirectory to specify the copy destination.

  • resources:testResources copies the resources for the test source code to the test output directory.

    This goal usually executes automatically, because it is bound by default to the process-test-resources life-cycle phase. It always uses the project.build.testResources element to specify the resources, and by default uses the project.build.testOutputDirectory to specify the copy destination.

  • resources:copy-resources copies resources to an output directory.

    This goal requires that you configure the resources to be copied, and specify the outputDirectory.

Example

Profile 을 이용하여 deploy 환경에 따라 패키징

  1. maven project 생성

    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
  2. pom.xml 에 properties 추가

    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- 기본 프로파일 -->
            <env>local</env>
    	</properties>
  3. profiles 기술

    <profiles>
    		<profile>
    			<id>local</id>
    			<activation>
    				<property>
    					<name>env</name>
    					<value>local</value>
    				</property>
    			</activation>
    			<properties>
    				<env>local</env>
    			</properties>
    		</profile>
    		<profile>
    			<id>dev</id>
    			<activation>
    				<property>
    					<name>env</name>
    					<value>dev</value>
    				</property>
    			</activation>
    			<properties>
    				<env>dev</env>
    			</properties>
    		</profile>
    		<profile>
    			<id>test</id>
    			<activation>
    				<property>
    					<name>env</name>
    					<value>test</value>
    				</property>
    			</activation>
    			<properties>
    				<env>test</env>
    			</properties>
    		</profile>
    		<profile>
    			<id>real</id>
    			<activation>
    				<property>
    					<name>env</name>
    					<value>real</value>
    				</property>
    			</activation>
    			<properties>
    				<env>real</env>
    			</properties>
    		</profile>
    	</profiles>
  4. build 항목에 추가

    <build>
    		<finalName>ArtifactId</finalName>
    		<resources>
    			<resource>
    				<directory>src/main/resources/${env}</directory>
    			</resource>
    		</resources>
    		<testResources>
    			<testResource>
    				<directory>src/test/resources/${env}</directory>
    			</testResource>
    		</testResources>
  5. pom.xml 에 war-plugin 의 webResources에 targetPath 와 resource 경로 설정

    <!-- aaaa -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
        <webResources>
            <resource>
                <directory>src/main/filters/${env}</directory>
                <includes>  
                    <include>*.properties</include>
                    <include>*.xml</include>
                </includes> 
                <targetPath>WEB-INF/config</targetPath>
                <filtering>true</filtering>
            </resource>
        </webResources>
        </configuration>
    </plugin>
  6. 다음과 같은 폴더 구조 생성

     Click here to expand...
    • src
      • main
        • java
      • resources
        • dev
          • jdbc.properties
          • log4j.properties
          • database-context.xml
        • local
          • jdbc.properties
          • log4j.properties
          • database-context.xml
        • real
          • jdbc.properties
          • log4j.properties
          • database-context.xml
        • test
          • jdbc.properties
          • log4j.properties
          • database-context.xml
        • webapp
      • resources
        • default
          • css
          • images
          • js
          • html5
          • jqgrid
          • jquery
    • test
      • java
      • resources
  7. mvn clean package -P profilename

 

jar plugin 에 사용

  1. profiles 기술 (위 3번 항목)
  2. 33

      <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>MyMainClass</mainClass>
                                <packageName>
                                </packageName>
                            </manifest>
                            <manifestEntries>
                                <mode>development</mode>
                                <url>${project.url}</url>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
  3. resources 항목에 기술

     <build>
        <resources>
            <resource>
                <directory>src/main/resources/${env}</directory>
             </resource>
        </resources>
     </build>
  4. src/main/resources 에 다음과 같은 폴더를 만들고 리소스 파일 위치
    1. local
    2. dev
    3. test
    4. real
  5. mvn package -P{local, dev, test, real}

참고자료