gradle测试与线上打包
首先,第一反应理所当然的是profile :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd " default-lazy-init="true"> <description>Spring</description> <!--已经用注解实现了,请查看RedisConfig--> <!--<import resource="classpath:applicationContext-redis.xml"/>--> <!--<import resource="classpath:applicationContext-mongo.xml"/>--> <!--已经修改为通过注解配置--> <!--<context:component-scan base-package="*********" />--> <!--线上配置读写分离的时候用这个 start --> <bean id="writeDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${master.jdbc.driver}" /> <property name="url" value="${master.jdbc.url}" /> <property name="username" value="${master.jdbc.username}" /> <property name="password" value="${master.jdbc.password}" /> <property name="maxActive" value="${maxActive}" /> <property name="minIdle" value="${maxIdle}" /> </bean> <bean id="readDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${slave.jdbc.driver}" /> <property name="url" value="${slave.jdbc.url}" /> <property name="username" value="${slave.jdbc.username}" /> <property name="password" value="${slave.jdbc.password}" /> <property name="maxActive" value="${maxActive}" /> <property name="minIdle" value="${maxIdle}" /> </bean> <bean id="dataSource" class="---------------------------------------Source" primary="true"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="write" value-ref="writeDataSource"/> <entry key="read" value-ref="readDataSource"/> </map> </property> <property name="defaultTargetDataSource" ref="writeDataSource"/> </bean> <!--线上配置读写分离的时候用这个 end --> <!-- 配置mybatis的sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mappers.xml文件 ,要加上classpath:com/... --> <property name="mapperLocations" value="classpath*:-----/**/*Mapper.xml"></property> <!-- mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="checkLock*" propagation="REQUIRES_NEW" isolation="READ_COMMITTED" /> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="new*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="create*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="register*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> <tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- aop事务配置 --> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))" /><!-- (public * com.*.service..*.*(..))--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!-- 启用事务注释支持 --> <tx:annotation-driven transaction-manager="transactionManager" /> <beans profile="production"> <context:property-placeholder location="classpath*:production/*"/> <bean id="propertyConfigurer" class="---------------------------.EncryptablePropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:production/jdbc.properties</value> <value>classpath:production/application.properties</value> <value>classpath:production/readDBPath.properties</value> <value>classpath:production/localswitch.properties</value> <value>classpath:production/redisCache.properties</value> </list> </property> </bean> </beans> <beans profile="test"> <context:property-placeholder location="classpath*:test/*"/> <!-- 属性文件读入 --> <bean id="propertyConfigurer-test" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:test/jdbc.properties</value> <value>classpath:test/application.properties</value> <value>classpath:test/readDBPath.properties</value> <value>classpath:test/localswitch.properties</value> <value>classpath:test/redisCache.properties</value> </list> </property> </bean> </beans> </beans>
然而,这个方法有点不太理想,因为似乎只能支持文本类型比如properties什么的,即使不用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,然后我们用的gradle,于是又有以下方法(使用gradle自定义打包命令):
group 'project' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'maven' sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { mavenLocal() maven{ url 'http://192.168.1.-:8081/nexus/content/repositories/thirdparty/' } maven{ url 'http://192.168.1.-:8081/nexus/content/groups/public/' } } dependencies { compile('---:spring-boot-dubbox-starter:0.0.1'){ exclude module: "slf4j-log4j12" } compile( [group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.4.0.RELEASE'], [group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '1.4.0.RELEASE'], // [group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.4.0.RELEASE'], project(":p2p-support"), project(":p2p-api") ) testCompile( // [group: 'junit', name: 'junit', version: '4.11'] ) } jar { baseName = '---' version = '1.0-SNAPSHOT' destinationDir=file("$buildDir/libs") } // 项目依赖包拷贝 task copyDependency(type : Copy){ from configurations.compile into "$buildDir/output/dependency" } // 复制Dockerfile 文件 task copyDockerfile(type : Copy){ from("src/main/resources/docker/Dockerfile") into "zip" } def copyMainClass = copySpec{ from ("$buildDir/classes/main") into "classes" } def copyAllLib = copySpec{ from ("$buildDir/output/dependency") into "lib" } def copyConf = copySpec{ from (sourceSets.main.resources.srcDirs){ include "*.xml" include "cert/**" include "META-INF/**" include "templates/**" include "*.properties" } into "conf" } def copyBin = copySpec{ from (sourceSets.main.resources.srcDirs){ include "bin/*.sh" } } // 打包 task zipProject(type:Zip, dependsOn : [jar, copyDependency] ){ with copyAllLib with copyMainClass with copyConf with copyBin into baseName destinationDir=file("zip") } def copyProductionConf = copySpec{ from ("package/production"){ include "*.xml" include "cert/**" include "META-INF/**" include "templates/**" include "*.properties" } into "conf" } // build product project task zipProductProject(type:Zip, dependsOn : [jar, copyDependency] ){ with copyAllLib with copyMainClass with copyProductionConf with copyBin into baseName destinationDir=file("zip") } def copyTestConf = copySpec{ from ("package/test"){ include "*.xml" include "cert/**" include "META-INF/**" include "templates/**" include "*.properties" } into "conf" } // build product project task zipTestProject(type:Zip, dependsOn : [jar, copyDependency] ){ with copyAllLib with copyMainClass with copyTestConf with copyBin into baseName destinationDir=file("zip") }
项目结构:
转载请注明出处