Java maven 打包配置分离

1、配置文件目录结构:



dev为开发环境配置,其中和sit有差别的配置文件是application.properties,jdbc.properties,redis.properties、以及需要维护两份spring配置文件。

2、spring配置分离(如果spring-context.xml有更新,需要维护两份 - -!)

dev环境下spring-spring-context.xml配置读取classpath下的资源文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>classpath:application.properties</value>
				<value>classpath:jdbc.properties</value>
				<value>classpath:redis.properties</value>
			</array>
		</property>
	</bean>
SIT 环境下spring- spring-context.xml配置读取sit服务器上的资源文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>file:/bqhexin/conf/rcbms/application.properties</value>
				<value>file:/bqhexin/conf/rcbms/jdbc.properties</value>
				<value>file:/bqhexin/conf/rcbms/redis.properties</value>
			</array>
		</property>
	</bean>


web.xml配置:

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
             classpath:META-INF/spring/spring-context.xml,
             classpath:META-INF/mybatis/spring-mybatis.xml
        </param-value>
	</context-param>




3、maven配置

profiles配置:

<profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>sit</id>
            <properties>
                <package.environment>sit</package.environment>
            </properties>
        </profile>
    </profiles>

build:

<build>
		<finalName>rcbms</finalName>
		<!--<sourceDirectory>src</sourceDirectory>-->
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
					<!--<warSourceDirectory>WebRoot</warSourceDirectory>-->
					<!--<archive>-->
						<!--<addMavenDescriptor>false</addMavenDescriptor>-->
					<!--</archive>-->
					<!--<warName>Cms</warName>-->
					<webResources>
						<resource>
							<directory>src/main/resources/${package.environment}/</directory>
							<targetPath>WEB-INF/classes</targetPath>
							<filtering>true</filtering>
						</resource>
					</webResources>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
			</resource>
		</resources>
	</build>


<directory>src/main/resources/${package.environment}/</directory>不能写成绝对路径<directory>/src/main/resources/${package.environment}/</directory>,Linux系统下找不到的。

4、打包测试

在项目根目录下打开DOS窗口使用打包命令mvn clean package -P sit或者mvn clean package -P dev,就可以打出不同配置的包





Maven 是一个构建工具,用于管理和构建 Java 项目的依赖关系。在 Maven 中,lib 是指项目所需的第三方库文件,而 jar 是指项目本身的可执行文件。 在 Maven 中,可以通过配置来实现 lib 和 jar 分离打包。主要的步骤如下: 1. 在项目的 pom.xml 中,配置 Maven 的插件和属性。 2. 在 pom.xml 中,通过配置 dependencies 标签来管理项目所需的依赖库。将需要的库文件的 groupId、artifactId 和版本号等信息填写在 dependencies 中。 3. 设置打包方式。通过配置 Maven 的插件,将项目编译为一个可执行的 jar 文件,并将所有依赖的库文件打包为一个单独的 lib 目录。 4. 配置 Maven 的插件实现打包。可以使用 Maven 自带的插件如 maven-jar-plugin 和 maven-dependency-plugin 来实现分离打包。 5. 运行 Maven 命令进行项目构建和打包。 在执行以上步骤后,Maven 会根据 pom.xml 中的配置,将项目本身的可执行 jar 文件和所有的依赖库文件分别打包。项目本身的 jar 文件将通过 maven-jar-plugin 插件生成,并存放在 target 目录下。而所有的依赖库文件将通过 maven-dependency-plugin 插件生成,并存放在 target 目录下的 lib 目录中。 通过以上的操作,我们可以实现 lib 和 jar 分离打包。这样,在部署项目时,只需要将项目本身的 jar 文件和 lib 目录一起拷贝到目标环境中即可。这样可以减小发布包的大小,并且便于管理和维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值