SpringBoot 动态打包
配置pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>1.5.19.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.zuoye</groupId> 12 <artifactId>springboot-quickstart-1</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot-quickstart-1</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <!-- profile 配置切换: mvn clean install -P dev / prod / local / test / --> 22 23 <profiles> 24 <profile> 25 <id>dev</id> 26 <activation> 27 <activeByDefault>true</activeByDefault> 28 </activation> 29 <dependencies> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-undertow</artifactId> 33 </dependency> 34 </dependencies> 35 <properties> 36 <spring.profiles.active>dev</spring.profiles.active> 37 <bfxy.dynamic>2018-001</bfxy.dynamic> 38 </properties> 39 </profile> 40 41 <profile> 42 <id>prod</id> 43 <dependencies> 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-undertow</artifactId> 47 </dependency> 48 </dependencies> 49 <properties> 50 <spring.profiles.active>prod</spring.profiles.active> 51 <bfxy.dynamic>2018-002</bfxy.dynamic> 52 </properties> 53 </profile> 54 </profiles> 55 56 <dependencies> 57 <dependency> 58 <groupId>org.springframework.boot</groupId> 59 <artifactId>spring-boot-starter-web</artifactId> 60 </dependency> 61 62 <dependency> 63 <groupId>org.springframework.boot</groupId> 64 <artifactId>spring-boot-starter-test</artifactId> 65 <scope>test</scope> 66 </dependency> 67 </dependencies> 68 69 70 71 <build> 72 <plugins> 73 <plugin> 74 <groupId>org.springframework.boot</groupId> 75 <artifactId>spring-boot-maven-plugin</artifactId> 76 </plugin> 77 78 <!-- 解解决maven update project 后版本降低为1.5的bug --> 79 <plugin> 80 <groupId>org.apache.maven.plugins</groupId> 81 <artifactId>maven-compiler-plugin</artifactId> 82 <configuration> 83 <source>1.8</source> 84 <target>1.8</target> 85 </configuration> 86 </plugin> 87 88 <!-- MAVEN打包时动态切换: mvn clean package -P prod/dev/local --> 89 <plugin> 90 <groupId>org.apache.maven.plugins</groupId> 91 <artifactId>maven-resources-plugin</artifactId> 92 <executions> 93 <execution> 94 <id>default-resources</id> 95 <phase>validate</phase> 96 <goals> 97 <goal>copy-resources</goal> 98 </goals> 99 <configuration> 100 <outputDirectory>target/classes</outputDirectory> 101 <useDefaultDelimiters>false</useDefaultDelimiters> 102 <delimiters> 103 <delimiter>$</delimiter> 104 </delimiters> 105 <resources> 106 <!-- 打包时包含properties、xml --> 107 <resource> 108 <directory>src/main/java</directory> 109 <includes> 110 <include>**/*.properties</include> 111 <include>**/*.xml</include> 112 </includes> 113 <!-- 是否替换资源中的属性--> 114 <filtering>true</filtering> 115 </resource> 116 <resource> 117 <directory>src/main/resources/</directory> 118 <filtering>true</filtering> 119 <includes> 120 <include>**/*.xml</include> 121 <include>**/*.yml</include> 122 <include>**/*.properties</include> 123 </includes> 124 </resource> 125 <resource> 126 <directory>src/main/resources/</directory> 127 <filtering>false</filtering> 128 <excludes> 129 <exclude>**/*.xml</exclude> 130 <exclude>**/*.yml</exclude> 131 <include>**/*.properties</include> 132 </excludes> 133 </resource> 134 </resources> 135 </configuration> 136 </execution> 137 </executions> 138 </plugin> 139 <!-- 单元测试 --> 140 <plugin> 141 <groupId>org.apache.maven.plugins</groupId> 142 <artifactId>maven-surefire-plugin</artifactId> 143 <configuration> 144 <skip>true</skip> 145 <includes> 146 <include>**/*Test*.java</include> 147 </includes> 148 <testFailureIgnore>true</testFailureIgnore> 149 </configuration> 150 </plugin> 151 <plugin> 152 <groupId>org.apache.maven.plugins</groupId> 153 <artifactId>maven-source-plugin</artifactId> 154 <executions> 155 <!-- 绑定到特定的生命周期之后,运行maven-source-pluin 运行目标为jar-no-fork --> 156 <execution> 157 <phase>package</phase> 158 <goals> 159 <goal>jar-no-fork</goal> 160 </goals> 161 </execution> 162 </executions> 163 </plugin> 164 </plugins> 165 </build> 166 167 </project>
配置文件.properties
my-config.properties
1 bfxy.title=bfxy 2 bfxy.name=hello spring boot! 3 bfxy.attr=12345 4 # 读取pom.xml中的配置 5 bfxy.dynamic=$bfxy.dynamic$
application-dev.properties
1 server.port=8002
application-prod.properties
1 server.port=8003
application.properties
1 spring.application.name=/quickstart 2 3 server.context-path=/qs 4 server.port=8001 5 6 custom.group=12345 7 custom.team=67890 8 9 #application-{}.properties //$spring.profiles.active$ 10 #$ 通配符在pom.xml中指定 11 spring.profiles.active=prod
打包或运行哪个换将修改application.properties的spring.profiles.active=即可