|
| 1 | +# IntelliJ IDEA 配合 Maven 的一些技巧 |
| 2 | + |
| 3 | +## 环境 |
| 4 | + |
| 5 | +- IntelliJ IDEA 2017.1 |
| 6 | +- Maven 3.3.9 |
| 7 | +- Nexus 3.2.1 |
| 8 | + |
| 9 | +## 学习前提 |
| 10 | + |
| 11 | +- 了解 Maven 配置的基本用法 |
| 12 | +- 了解私有仓库,比如 nexus 的一些概念 |
| 13 | + |
| 14 | +### Maven 中的 profile |
| 15 | + |
| 16 | +- Maven 中有一个概念叫做:`profile`,它的诞生主要是为了解决不同环境所需的不同变量、配置等问题。 |
| 17 | +- 有了 profile,可以根据激活的条件,启动不同条件下的配置信息。 |
| 18 | +- profile 是可以有多个的,也可以同时激活多个 profile,方便只有组合。 |
| 19 | +- profile 一般可以在三个地方:settings.xml,pom.xml,profiles.xml(这个不常用) |
| 20 | +- 在 settings.xml 上,一般大家用来做仓库的选择,比如以下 settings.xml 代码: |
| 21 | + |
| 22 | +``` xml |
| 23 | +<?xml version="1.0" encoding="UTF-8"?> |
| 24 | + |
| 25 | +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" |
| 26 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 27 | + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> |
| 28 | + |
| 29 | + <localRepository>D:\maven\my_local_repository</localRepository> |
| 30 | + |
| 31 | + <pluginGroups> |
| 32 | + </pluginGroups> |
| 33 | + |
| 34 | + <proxies> |
| 35 | + </proxies> |
| 36 | + |
| 37 | + <profiles> |
| 38 | + <profile> |
| 39 | + <id>nexus</id> |
| 40 | + <repositories> |
| 41 | + <repository> |
| 42 | + <id>nexus</id> |
| 43 | + <url>http://192.168.1.73:8081/repository/maven-public/</url> |
| 44 | + <releases> |
| 45 | + <enabled>true</enabled> |
| 46 | + </releases> |
| 47 | + <snapshots> |
| 48 | + <enabled>true</enabled> |
| 49 | + </snapshots> |
| 50 | + </repository> |
| 51 | + </repositories> |
| 52 | + <pluginRepositories> |
| 53 | + <pluginRepository> |
| 54 | + <id>nexus</id> |
| 55 | + <url>http://192.168.1.73:8081/repository/maven-public/</url> |
| 56 | + <releases> |
| 57 | + <enabled>true</enabled> |
| 58 | + </releases> |
| 59 | + <snapshots> |
| 60 | + <enabled>true</enabled> |
| 61 | + </snapshots> |
| 62 | + </pluginRepository> |
| 63 | + </pluginRepositories> |
| 64 | + </profile> |
| 65 | + <profile> |
| 66 | + <id>aliyun</id> |
| 67 | + <repositories> |
| 68 | + <repository> |
| 69 | + <id>aliyun</id> |
| 70 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 71 | + <releases> |
| 72 | + <enabled>true</enabled> |
| 73 | + </releases> |
| 74 | + <snapshots> |
| 75 | + <enabled>true</enabled> |
| 76 | + </snapshots> |
| 77 | + </repository> |
| 78 | + </repositories> |
| 79 | + <pluginRepositories> |
| 80 | + <pluginRepository> |
| 81 | + <id>aliyun</id> |
| 82 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 83 | + <releases> |
| 84 | + <enabled>true</enabled> |
| 85 | + </releases> |
| 86 | + <snapshots> |
| 87 | + <enabled>true</enabled> |
| 88 | + </snapshots> |
| 89 | + </pluginRepository> |
| 90 | + </pluginRepositories> |
| 91 | + </profile> |
| 92 | + </profiles> |
| 93 | + |
| 94 | + <activeProfiles> |
| 95 | + <activeProfile>nexus</activeProfile> |
| 96 | + </activeProfiles> |
| 97 | + |
| 98 | +</settings> |
| 99 | +``` |
| 100 | + |
| 101 | +- 以上代码中 profile 就做一件事:设置全局的 profile,一个是 nexus 仓库,一个是 aliyun 仓库,默认激活的是 nexus 仓库。(activeProfiles) |
| 102 | +- 在 pom.xml 中,一般用来激活环境配置,比如: |
| 103 | + |
| 104 | +``` xml |
| 105 | +<profiles> |
| 106 | + <profile> |
| 107 | + <id>dev</id> |
| 108 | + <properties> |
| 109 | + <package.environment>dev</package.environment> |
| 110 | + </properties> |
| 111 | + <activation> |
| 112 | + <activeByDefault>true</activeByDefault> |
| 113 | + </activation> |
| 114 | + <build> |
| 115 | + <resources> |
| 116 | + <resource> |
| 117 | + <directory>src/main/resources</directory> |
| 118 | + <includes> |
| 119 | + <include>**/*</include> |
| 120 | + </includes> |
| 121 | + <filtering>true</filtering> |
| 122 | + </resource> |
| 123 | + <resource> |
| 124 | + <directory>src/main/env/${package.environment}</directory> |
| 125 | + <includes> |
| 126 | + <include>**/*</include> |
| 127 | + </includes> |
| 128 | + <filtering>true</filtering> |
| 129 | + </resource> |
| 130 | + </resources> |
| 131 | + <finalName>${project.artifactId}</finalName> |
| 132 | + </build> |
| 133 | + </profile> |
| 134 | + <profile> |
| 135 | + <id>product</id> |
| 136 | + <properties> |
| 137 | + <package.environment>product</package.environment> |
| 138 | + </properties> |
| 139 | + <activation> |
| 140 | + <activeByDefault>false</activeByDefault> |
| 141 | + </activation> |
| 142 | + <build> |
| 143 | + <resources> |
| 144 | + <resource> |
| 145 | + <directory>src/main/resources</directory> |
| 146 | + <includes> |
| 147 | + <include>**/*</include> |
| 148 | + </includes> |
| 149 | + <filtering>true</filtering> |
| 150 | + </resource> |
| 151 | + <resource> |
| 152 | + <directory>src/main/env/${package.environment}</directory> |
| 153 | + <includes> |
| 154 | + <include>**/*</include> |
| 155 | + </includes> |
| 156 | + <filtering>true</filtering> |
| 157 | + </resource> |
| 158 | + </resources> |
| 159 | + <finalName>${project.artifactId}</finalName> |
| 160 | + </build> |
| 161 | + </profile> |
| 162 | +</profiles> |
| 163 | +``` |
| 164 | + |
| 165 | +- 以上代码中 profile 就做一件事:打包的时候,默认是 dev 模式,打包 src/main/env/dev 下的配置文件,如果选择 product 则打包 src/main/env/product 下的配置文件 |
| 166 | + |
| 167 | +### IntelliJ IDEA 使用 Maven Profile 的案例 |
| 168 | + |
| 169 | +- 在 IntelliJ IDEA 上调用 profile 简单,如下图勾选对应的复选框即可,可以多选。 |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +- 只使用 aliyun 仓库可以这样配置 settings.xml: |
| 174 | + |
| 175 | +``` xml |
| 176 | +<?xml version="1.0" encoding="UTF-8"?> |
| 177 | + |
| 178 | +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" |
| 179 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 180 | + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> |
| 181 | + |
| 182 | + <localRepository>D:\maven\my_local_repository</localRepository> |
| 183 | + |
| 184 | + <pluginGroups> |
| 185 | + </pluginGroups> |
| 186 | + |
| 187 | + <proxies> |
| 188 | + </proxies> |
| 189 | + |
| 190 | + <profiles> |
| 191 | + <profile> |
| 192 | + <id>aliyun</id> |
| 193 | + <repositories> |
| 194 | + <repository> |
| 195 | + <id>aliyun</id> |
| 196 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 197 | + <releases> |
| 198 | + <enabled>true</enabled> |
| 199 | + </releases> |
| 200 | + <snapshots> |
| 201 | + <enabled>true</enabled> |
| 202 | + </snapshots> |
| 203 | + </repository> |
| 204 | + </repositories> |
| 205 | + <pluginRepositories> |
| 206 | + <pluginRepository> |
| 207 | + <id>aliyun</id> |
| 208 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 209 | + <releases> |
| 210 | + <enabled>true</enabled> |
| 211 | + </releases> |
| 212 | + <snapshots> |
| 213 | + <enabled>true</enabled> |
| 214 | + </snapshots> |
| 215 | + </pluginRepository> |
| 216 | + </pluginRepositories> |
| 217 | + </profile> |
| 218 | + </profiles> |
| 219 | + |
| 220 | + <activeProfiles> |
| 221 | + <activeProfile>aliyun</activeProfile> |
| 222 | + </activeProfiles> |
| 223 | + |
| 224 | +</settings> |
| 225 | +``` |
| 226 | + |
| 227 | +- 使用 nexus + aliyun 仓库可以这样配置 settings.xml: |
| 228 | + |
| 229 | +``` xml |
| 230 | +<?xml version="1.0" encoding="UTF-8"?> |
| 231 | + |
| 232 | +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" |
| 233 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 234 | + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> |
| 235 | + |
| 236 | + <localRepository>D:\maven\my_local_repository</localRepository> |
| 237 | + |
| 238 | + <pluginGroups> |
| 239 | + </pluginGroups> |
| 240 | + |
| 241 | + <proxies> |
| 242 | + </proxies> |
| 243 | + |
| 244 | + <profiles> |
| 245 | + <profile> |
| 246 | + <id>nexus</id> |
| 247 | + <repositories> |
| 248 | + <repository> |
| 249 | + <id>nexus</id> |
| 250 | + <url>http://192.168.1.73:8081/repository/maven-public/</url> |
| 251 | + <releases> |
| 252 | + <enabled>true</enabled> |
| 253 | + </releases> |
| 254 | + <snapshots> |
| 255 | + <enabled>true</enabled> |
| 256 | + </snapshots> |
| 257 | + </repository> |
| 258 | + </repositories> |
| 259 | + <pluginRepositories> |
| 260 | + <pluginRepository> |
| 261 | + <id>nexus</id> |
| 262 | + <url>http://192.168.1.73:8081/repository/maven-public/</url> |
| 263 | + <releases> |
| 264 | + <enabled>true</enabled> |
| 265 | + </releases> |
| 266 | + <snapshots> |
| 267 | + <enabled>true</enabled> |
| 268 | + </snapshots> |
| 269 | + </pluginRepository> |
| 270 | + </pluginRepositories> |
| 271 | + </profile> |
| 272 | + <profile> |
| 273 | + <id>aliyun</id> |
| 274 | + <repositories> |
| 275 | + <repository> |
| 276 | + <id>aliyun</id> |
| 277 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 278 | + <releases> |
| 279 | + <enabled>true</enabled> |
| 280 | + </releases> |
| 281 | + <snapshots> |
| 282 | + <enabled>true</enabled> |
| 283 | + </snapshots> |
| 284 | + </repository> |
| 285 | + </repositories> |
| 286 | + <pluginRepositories> |
| 287 | + <pluginRepository> |
| 288 | + <id>aliyun</id> |
| 289 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 290 | + <releases> |
| 291 | + <enabled>true</enabled> |
| 292 | + </releases> |
| 293 | + <snapshots> |
| 294 | + <enabled>true</enabled> |
| 295 | + </snapshots> |
| 296 | + </pluginRepository> |
| 297 | + </pluginRepositories> |
| 298 | + </profile> |
| 299 | + </profiles> |
| 300 | + |
| 301 | + <activeProfiles> |
| 302 | + <activeProfile>nexus</activeProfile> |
| 303 | + </activeProfiles> |
| 304 | + |
| 305 | +</settings> |
| 306 | +``` |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | + |
| 313 | + |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
| 320 | + |
| 321 | + |
| 322 | + |
| 323 | + |
| 324 | + |
| 325 | + |
| 326 | + |
| 327 | + |
| 328 | + |
| 329 | + |
| 330 | + |
| 331 | + |
| 332 | + |
| 333 | + |
| 334 | + |
| 335 | + |
| 336 | + |
| 337 | + |
| 338 | + |
| 339 | + |
0 commit comments