提交 dcd7ee6e 编写于 作者: Z zhangchaohuang

2017-06-21 增加 Maven 搭配 IntelliJ IDEA 的使用技巧

上级 ab283af6
......@@ -46,23 +46,24 @@
- [21. 插件相关功能](plugins-settings.md)
- [22. Eclipse 的 Java Web 项目环境搭建](eclipse-java-web-project-introduce.md)
- [23. Maven 结构项目搭建](maven-project-introduce.md)
- [24. Maven 的单模块 / 多模块之 Spring MVC + Spring + Mybatis 项目讲解(重点)](maven-java-web-project-introduce.md)
- [25. Maven 的单模块之 Spring MVC + Spring + Spring Data JPA 项目(基于 IntelliJ IDEA)](maven-java-web-project-introduce2.md)
- [26. Debug 技巧](debug-introduce.md)
- [27. 重构技巧](refactor-introduce.md)
- [28. 数据库管理工具](database-introduce.md)
- [29. IntelliJ IDEA 常用细节设置-1](settings-introduce-1.md)
- [30. IntelliJ IDEA 常用细节设置-2](settings-introduce-2.md)
- [31. IntelliJ IDEA 常用细节设置-3](settings-introduce-3.md)
- [32. IntelliJ IDEA 常用细节设置-4](settings-introduce-4.md)
- [33. IntelliJ IDEA 常用快捷键讲解(Win + Linux)(新用户必看)](keymap-introduce.md)
- [34. IntelliJ IDEA 常用快捷键讲解(Mac)(新用户必看)](keymap-mac-introduce.md)
- [35. 从 Windows 过度到 Mac 必备快捷键对照表(新用户必看)](keymap-win-mac.md)
- [36. IntelliJ IDEA 的 Java 热部署插件 JRebel 安装及使用](jrebel-setup.md)
- [37. IntelliJ IDEA 远程调试(Tomcat+Jetty)](remote-debugging.md)
- [38. 最特殊的快捷键 Alt + Enter 介绍(新用户必看)](hotkey-alt-enter-introduce.md)
- [39. IntelliJ IDEA 插件开发视频教程](plugins-develop.md)
- [40. 本教程总结](this-tutorial-the-end.md)
- [24. IntelliJ IDEA 配合 Maven 的一些要点](maven-skill-introduce.md)
- [25. Maven 的单模块 / 多模块之 Spring MVC + Spring + Mybatis 项目讲解(重点)](maven-java-web-project-introduce.md)
- [26. Maven 的单模块之 Spring MVC + Spring + Spring Data JPA 项目(基于 IntelliJ IDEA)](maven-java-web-project-introduce2.md)
- [27. Debug 技巧](debug-introduce.md)
- [28. 重构技巧](refactor-introduce.md)
- [29. 数据库管理工具](database-introduce.md)
- [30. IntelliJ IDEA 常用细节设置-1](settings-introduce-1.md)
- [31. IntelliJ IDEA 常用细节设置-2](settings-introduce-2.md)
- [32. IntelliJ IDEA 常用细节设置-3](settings-introduce-3.md)
- [33. IntelliJ IDEA 常用细节设置-4](settings-introduce-4.md)
- [34. IntelliJ IDEA 常用快捷键讲解(Win + Linux)(新用户必看)](keymap-introduce.md)
- [35. IntelliJ IDEA 常用快捷键讲解(Mac)(新用户必看)](keymap-mac-introduce.md)
- [36. 从 Windows 过度到 Mac 必备快捷键对照表(新用户必看)](keymap-win-mac.md)
- [37. IntelliJ IDEA 的 Java 热部署插件 JRebel 安装及使用](jrebel-setup.md)
- [38. IntelliJ IDEA 远程调试(Tomcat+Jetty)](remote-debugging.md)
- [39. 最特殊的快捷键 Alt + Enter 介绍(新用户必看)](hotkey-alt-enter-introduce.md)
- [40. IntelliJ IDEA 插件开发视频教程](plugins-develop.md)
- [41. 本教程总结](this-tutorial-the-end.md)
## 联系(Contact)
......
# IntelliJ IDEA 配合 Maven 的一些技巧
## 环境
- IntelliJ IDEA 2017.1
- Maven 3.3.9
- Nexus 3.2.1
## 学习前提
- 了解 Maven 配置的基本用法
- 了解私有仓库,比如 nexus 的一些概念
### Maven 中的 profile
- Maven 中有一个概念叫做:`profile`,它的诞生主要是为了解决不同环境所需的不同变量、配置等问题。
- 有了 profile,可以根据激活的条件,启动不同条件下的配置信息。
- profile 是可以有多个的,也可以同时激活多个 profile,方便只有组合。
- profile 一般可以在三个地方:settings.xml,pom.xml,profiles.xml(这个不常用)
- 在 settings.xml 上,一般大家用来做仓库的选择,比如以下 settings.xml 代码:
``` xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\maven\my_local_repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
```
- 以上代码中 profile 就做一件事:设置全局的 profile,一个是 nexus 仓库,一个是 aliyun 仓库,默认激活的是 nexus 仓库。(activeProfiles)
- 在 pom.xml 中,一般用来激活环境配置,比如:
``` xml
<profiles>
<profile>
<id>dev</id>
<properties>
<package.environment>dev</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/env/${package.environment}</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<finalName>${project.artifactId}</finalName>
</build>
</profile>
<profile>
<id>product</id>
<properties>
<package.environment>product</package.environment>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/env/${package.environment}</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<finalName>${project.artifactId}</finalName>
</build>
</profile>
</profiles>
```
- 以上代码中 profile 就做一件事:打包的时候,默认是 dev 模式,打包 src/main/env/dev 下的配置文件,如果选择 product 则打包 src/main/env/product 下的配置文件
### IntelliJ IDEA 使用 Maven Profile 的案例
- 在 IntelliJ IDEA 上调用 profile 简单,如下图勾选对应的复选框即可,可以多选。
![IntelliJ IDEA 配合 Maven 的一些技巧](images/xxii-f-maven-skill-introduce.jpg)
- 只使用 aliyun 仓库可以这样配置 settings.xml:
``` xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\maven\my_local_repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<profiles>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>aliyun</activeProfile>
</activeProfiles>
</settings>
```
- 使用 nexus + aliyun 仓库可以这样配置 settings.xml:
``` xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\maven\my_local_repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册