Ticket #6 (new 需求)

Opened 14 years ago

Last modified 14 years ago

profile环境设置

Reported by: qinhongyuan Owned by: qinhongyuan
Priority: major Milestone: Maven插件
Component: Maven教程 Version:
Keywords: maven profile Cc: qinhongyuan
Due Date: 20/01/2012

Description

对开发环境和测试环境不同而自动改变打包的方式

Change History

comment:1 Changed 14 years ago by qinhongyuan

  • 普通目录配置处理(配置不同的环境变量)
    <build>
    ...
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    </resource>
    </resources>
    ...
    </build>
    

directory 表示你要过滤的那个文件
filtering 表示要过滤,不过滤就不会使用Maven的环境变量功能

<project>
<profiles>
...
<profile>
<id>dev</id>
<properties>
<变量名>变量值</变量名>
</properties>
<profile>
...
</profiles>
</project>

在resources目录下的文件都可以使用变量名
例如xxxx.properties文件
db.user=${变量名}

运行命令mvn package -Pdev即可
打包后的情况
db.user=变量名

comment:2 Changed 14 years ago by qinhongyuan

  • 同理可以设置其它目录下的文件,设置测试环境就写多一个profile就可以,使用mvn package -P+id就可以了
  • 当然这个是针对jar包的处理。。
  • web应用需要另外一种配置,修改某目录下的css中引用图片链接
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1-beta-1</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
    		    <webResources>
    			<resource>
    			    <filtering>true</filtering>
    			    <directory>src/main/webapp</directory>
    			    <includes>
    				<include>**/*.css</include>
    			    </includes>
    			</resource>
    		    </webResources>
                    </configuration>
                </plugin>
    
  • 在配置以后,profile配置和上面的配置一样就可以了
        <profiles>
    	<profile>
    	    <id>dev</id>
    	    <properties>
    		<UI_PLUGIN_ROOT>http://localhost.pconline.com.cn:12085/ubb2/images/ui_plugin</UI_PLUGIN_ROOT>
    	    </properties>
    	</profile>
        </profiles>
    
  • 构建前
    url(${UI_PLUGIN_ROOT}/ui-bg_flat_75_ffffff_40x100.png)
    
  • 构建后
    url(http://localhost.pconline.com.cn:12085/ubb2/images/ui_plugin/ui-bg_flat_75_ffffff_40x100.png)
    

comment:3 Changed 14 years ago by qinhongyuan

  • profile的作用还是不错的,可以让我们的程序按照不同的方式来构建项目
  • 这里我介绍一下使用profile来控制项目环境
  • 背景:UBB编辑器上,我们需要使用jstestdriver来对js代码进行自动化测试
  • 使用这个功能,我们可以写一个profile
    	<profile>
    	    <id>jstestdriver</id>
    	    <dependencies>
    		<dependency>
    		    <groupId>com.googlecode.jstd-maven-plugin</groupId>
    		    <artifactId>jstd-maven-plugin</artifactId>
    		    <version>1.3.2.5</version>
    		    <scope>test</scope>
    		</dependency>
    	    </dependencies>
    	    <build>
    		<plugins>
    		    <plugin>
    			<groupId>com.googlecode.jstd-maven-plugin</groupId>
    			<artifactId>jstd-maven-plugin</artifactId>
    			<version>1.3.2.5</version>
    			<configuration>
    			    <port>9876</port>
    			    <browser>${dpath}</browser>
    			</configuration>
    			<executions>
    			    <execution>
    				<id>run-tests</id>
    				<goals>
    				    <goal>test</goal>
    				</goals>
    			    </execution>
    			</executions>
    		    </plugin>
    		</plugins>
    	    </build>
    	</profile>
    
    clean package -Pdev-local,jstestdriver -Ddpath="C:\Program Files\Internet Explorer\iexplore.exe","C:\Documents and Settings\pc\Local Settings\Application Data\Google\Chrome\Application\chrome.exe","C:\Program Files\Mozilla Firefox\firefox.exe" 
    
  • 使用三个浏览器来进行自动测试 -P命令跟着的id是要激活的id,也就是说,你想要哪个功能可以通过id来控制。。
Last edited 14 years ago by qinhongyuan (previous) (diff)
Note: See TracTickets for help on using tickets.