maven 本地、测试、生产打包

本文介绍了一种利用Maven进行本地、测试及生产环境打包的方法。通过配置不同的资源路径和参数,实现不同环境下配置文件的灵活加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前一篇文章也是maven的本地、测试、生产打包的一种方式,而且网上大都也是这种方式,而这篇文章是领导要求这么弄的也就这么弄了,下面来看看

一、工程目录


注意一下两个红框哦,上面的那个框是没有改动的也不用改动而下面的这个红框中我们添加了一个_online 和_test 两个文件夹其中的配置的东西和resources是一样的只是路径、数据库地址什么的不一样而已,resources文件夹中的配置就是本地开发环境的配置而resources_online 就是线上环境的配置,resources_test就是测试环境上的配置。

注意:上篇文章中我们修改了上面的而这篇文章中修改了下面的

二、配置文件

1、默认资源加载路径配置:

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!--配置默认值-->
		 <package.target>test</package.target>
	</properties>
其中的package.target的值就是配置的默认值,这个值和resources_test、resources_online 这个有关,其实在这个demo中配置的值就是resources_的后缀。这个一定要配置不然要么报错要么打包大不了!!!

2、profiles配置:

<profiles>
        <profile>
            <id>test</id>
            <properties>
                <package.target>test</package.target>
            </properties>
        </profile>
        <profile>
            <id>online</id>
            <properties>
                <package.target>online</package.target>
            </properties>
        </profile>
    </profiles>
这里同样是配置了package.target的值,也就是文件的后缀,这一个也要配置,如果不配置的话使用默认的路劲打包是没问题的而如果使用非默认的路劲则打不了包!!!

所以说来说去这两个配置还是必须的……

三、打包:

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.1.1</version>
			 <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <!--动态定义war包名称,其中的${package.target}就是传过来的值,默认使用开始properties 中定义的package.target的值 -->
                    <warName>sas_bdwallet_${package.target}</warName>
                    <!--就是这里实现了加载不同目录下的配置文件和工程目录结构红框中的路劲是一样的  -->
                    <webResources>
                        <resource>
                            <directory>src/main/resources_${package.target}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <!--使用传递过来的参数 -->
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                    <!--工程下web.xml路劲,不管是什么打包这个都要配置不然会报错  -->
                    <webXml>WebRoot\WEB-INF\web.xml</webXml>
                    <!--js,css,jsp路劲配置,如果不添加则js、css、jsp都不在war包中  -->
                    <warSourceDirectory>WebRoot</warSourceDirectory>
                </configuration>
		</plugin>
	</plugins>
</build>

注释说的很清楚了,不说太多,但是要注意<webResources>中的<resource>中的<directory>这个标签的路劲就是我们配置文件的路劲,后缀就是我们文件中配置的默认路劲或者运行时传过来的值,这样完整的路劲匹配到我们的那个路劲就加载那个文件下的配置文件……

注意:这里的webResources 这个标签,因为上一篇文章中是直接<resources> 标签下的<resource>而这里是<webResources> 标签下的<resource>所以你应该知道了为什么一个是修改上面红框中的路径一个是修改下面红框中的路劲了……

到这里我们只能使用文件中配置的那个路劲来打包,也就是如果默认配置的是test(测试环境)那就是加载了resources_test这个目录下的配置文件,如果配置的是online则加载的是resources_online这个目录下的配置文件这样相比之前方便了很多但是也有点不爽,比如我天天打测试包比较多,而打正式包时要改打完还要改回来,不太方便下面我们看看run时传递参数的方式。

四、run config 传递参数



看图片很清除了,配置Goals的值,-P(p大写)后面是你传的参数,像我们工程中的profiles的profile也就配了test 和online两个值,所以这里-p后面的参数也就只能从这两个参数中选择,其实也就是resources_test、resources_online这个后缀的动态传值。这样就ok了你传test war包就是测试的配置文件 传online war包就是正式的配置文件!

到此所有的都ok了……

五、项目中这个pom.xml配置文件

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.baidu.wallet</groupId>
	<artifactId>sas_bdwallet</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>sas_bdwallet</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!--配置默认值-->
		 <package.target>test</package.target>
	</properties>

<!-- 项目所依赖的jar包 -->
	<dependencies>
	<!-- spring springmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.6</version>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.3.0</version>
		</dependency>
		<!-- mybatis 链接spring包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- mysql 驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.36</version>
		</dependency>
		<!--druid数据库连接池  -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.15</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.12</version>
		</dependency>
		<!--文件上传  -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<!--jackson-->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.6.1</version>
		</dependency>
		<!--json -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>

		<!--log -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>
		<!-- jsp c标签引入 -->
		<dependency>
			<groupId>org.apache.taglibs</groupId>
			<artifactId>taglibs-standard-spec</artifactId>
			<version>1.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.taglibs</groupId>
			<artifactId>taglibs-standard-impl</artifactId>
			<version>1.2.1</version>
		</dependency>

		<!--AES MD5加密编码 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- httpclient -->
		 <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
          <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0.1</version>
        </dependency>
		
		<!--测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<!--profile配置 -->
	<profiles>
        <profile>
            <id>test</id>
            <properties>
                <package.target>test</package.target>
            </properties>
        </profile>
        <profile>
            <id>online</id>
            <properties>
                <package.target>online</package.target>
            </properties>
        </profile>
    </profiles>
    
	<build>
	<plugins>
	    <!--编译插件配置-->
	    <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.7</source>
				<target>1.7</target>
			</configuration>
		</plugin>
		<!--打war包插件配置-->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.1.1</version>
			 <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <!--动态定义war包名称,其中的${package.target}就是传过来的值,默认使用开始properties 中定义的package.target的值 -->
                    <warName>sas_bdwallet_${package.target}</warName>
                    <!--就是这里实现了加载不同目录下的配置文件和工程目录结构红框中的路劲是一样的  -->
                    <webResources>
                        <resource>
                            <directory>src/main/resources_${package.target}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <!--使用传递过来的参数 -->
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                    <!--工程下web.xml路劲,不管是什么打包这个都要配置不然会报错  -->
                    <webXml>WebRoot\WEB-INF\web.xml</webXml>
                    <!--js,css,jsp路劲配置,如果不添加则js、css、jsp都不在war包中  -->
                    <warSourceDirectory>WebRoot</warSourceDirectory>
                </configuration>
		</plugin>
	</plugins>
</build>
</project>


<think>好的,我需要帮助用户了解如何使用Maven本地项目进行打包,特别是使用mvn package命令。首先,我需要确认用户的需求是什么。他们可能有一个本地Maven项目,想要生成可执行的JAR或WAR文件。接下来,我得整理出步骤,并确保每个步骤都清晰易懂。 用户提供的引用内容中提到了一些关键点,比如默认执行test命令的问题,解决方法是通过配置maven-surefire-plugin来跳过测试。另外,引用中还提到了在Eclipse中使用Maven打包,以及如何通过命令行执行mvn package。这些信息需要整合到回答中。 首先,我应该列出使用mvn package的基本步骤,确保用户了解前提条件,比如安装Maven正确配置pom.xml文件。然后,详细说明如何运行命令,可能遇到的问题比如测试失败,以及如何跳过测试。此外,用户可能需要知道如何在IDE中操作,比如Eclipse,所以需要包含相关步骤。 还要注意引用中的配置文件示例,正确展示如何跳过测试。同时,用户可能对输出文件的位置类型有疑问,需要说明target目录下的生成文件。最后,生成相关问题,帮助用户深入理解,比如性能优化、跳过测试的其他方法,以及处理依赖问题。 需要确保回答结构清晰,分步骤说明,引用正确的信息源,并在适当的位置添加引用标识。同时,语言要简洁明了,避免技术术语过于复杂,让新手也能理解。检查是否有遗漏的步骤,比如项目结构是否符合Maven标准,是否需要先执行clean等。最后,验证提供的命令配置是否正确,避免误导用户。</think>### 如何使用Maven本地项目进行打包 以下是使用`mvn package`命令打包本地Maven项目的详细步骤: #### 1. 前置条件 - 确保已安装Maven并配置环境变量(通过`mvn -v`验证)[^3]。 - 项目需符合Maven标准结构(包含`pom.xml`文件)。 #### 2. 基础打包命令 在项目根目录(含`pom.xml`的目录)执行: ```bash mvn package ``` 此命令会执行编译、测试(默认)并生成构建产物(如JAR/WAR)到`target/`目录[^3]。 #### 3. 跳过测试打包方式 若需跳过测试以提高速度或避免失败: ```bash mvn package -DskipTests ``` 或在`pom.xml`中永久配置(引用自示例配置): ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> ``` [^1] #### 4. 清理旧构建产物 推荐先清理历史构建: ```bash mvn clean package ``` 这会删除`target/`目录后重新构建[^2]。 #### 5. 在IDE中执行(以Eclipse为例) 1. 右键项目 -> Run As -> Maven build 2. Goals输入框填写`clean package` 3. 后续可通过历史配置快速执行 #### 6. 构建产物说明 - Java项目:默认生成`项目名-版本.jar` - Web项目:生成WAR文件 - 具体路径:`target/`目录下[^3] #### 7. 高级用法 包含测试报告的打包(需TestNG配置): ```bash mvn package -Dtestng.xml=testng.xml ``` 生成的可执行JAR可通过`java -jar target/xxx.jar`运行[^4]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值