• IntelliJ IDEA 配合 Maven 的一些技巧
    • 环境
    • 学习前提
      • Maven 中的 profile
      • IntelliJ IDEA 使用 Maven Profile 的案例

    IntelliJ IDEA 配合 Maven 的一些技巧

    环境

    • IntelliJ IDEA 2017.1
    • Maven 3.3.9
    • Nexus 3.2.1

    学习前提

    • 了解 Maven 配置的基本用法
    • 了解私有仓库,比如 nexus 的一些概念
    • 强烈建议把 Maven 的 settings.xml 文件同时放在:%USER_HOME%/.m2/settings.xml${maven.home}/conf/settings.xml 两个地方。避免使用终端的时候默认去调用用户目录下的

    Maven 中的 profile

    • Maven 中有一个概念叫做:profile,它的诞生主要是为了解决不同环境所需的不同变量、配置等问题。
    • 有了 profile,可以根据激活的条件,启动不同条件下的配置信息。
    • profile 是可以有多个的,也可以同时激活多个 profile,方便自由组合。
    • profile 一般可以在三个地方:settings.xml,pom.xml,profiles.xml(这个不常用)
    • 在 settings.xml 上,一般大家用来做仓库的选择,比如以下 settings.xml 代码:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    5. <localRepository>D:\maven\my_local_repository</localRepository>
    6. <pluginGroups>
    7. </pluginGroups>
    8. <proxies>
    9. </proxies>
    10. <profiles>
    11. <profile>
    12. <id>nexus</id>
    13. <repositories>
    14. <repository>
    15. <id>nexus</id>
    16. <url>http://192.168.1.73:8081/repository/maven-public/</url>
    17. <releases>
    18. <enabled>true</enabled>
    19. </releases>
    20. <snapshots>
    21. <enabled>true</enabled>
    22. </snapshots>
    23. </repository>
    24. </repositories>
    25. <pluginRepositories>
    26. <pluginRepository>
    27. <id>nexus</id>
    28. <url>http://192.168.1.73:8081/repository/maven-public/</url>
    29. <releases>
    30. <enabled>true</enabled>
    31. </releases>
    32. <snapshots>
    33. <enabled>true</enabled>
    34. </snapshots>
    35. </pluginRepository>
    36. </pluginRepositories>
    37. </profile>
    38. <profile>
    39. <id>aliyun</id>
    40. <repositories>
    41. <repository>
    42. <id>aliyun</id>
    43. <url>http://maven.aliyun.com/nexus/content/groups/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>aliyun</id>
    55. <url>http://maven.aliyun.com/nexus/content/groups/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. </profiles>
    66. <activeProfiles>
    67. <activeProfile>nexus</activeProfile>
    68. </activeProfiles>
    69. </settings>
    • 以上代码中 profile 就做一件事:设置全局的 profile,一个是 nexus 仓库,一个是 aliyun 仓库,默认激活的是 nexus 仓库。(activeProfiles)
    • 在 pom.xml 中,一般用来激活环境配置,比如以下代码:
    1. <profiles>
    2. <profile>
    3. <id>dev</id>
    4. <properties>
    5. <package.environment>dev</package.environment>
    6. </properties>
    7. <activation>
    8. <activeByDefault>true</activeByDefault>
    9. </activation>
    10. <build>
    11. <resources>
    12. <resource>
    13. <directory>src/main/resources</directory>
    14. <includes>
    15. <include>**/*</include>
    16. </includes>
    17. <filtering>true</filtering>
    18. </resource>
    19. <resource>
    20. <directory>src/main/env/${package.environment}</directory>
    21. <includes>
    22. <include>**/*</include>
    23. </includes>
    24. <filtering>true</filtering>
    25. </resource>
    26. </resources>
    27. <finalName>${project.artifactId}</finalName>
    28. </build>
    29. </profile>
    30. <profile>
    31. <id>product</id>
    32. <properties>
    33. <package.environment>product</package.environment>
    34. </properties>
    35. <activation>
    36. <activeByDefault>false</activeByDefault>
    37. </activation>
    38. <build>
    39. <resources>
    40. <resource>
    41. <directory>src/main/resources</directory>
    42. <includes>
    43. <include>**/*</include>
    44. </includes>
    45. <filtering>true</filtering>
    46. </resource>
    47. <resource>
    48. <directory>src/main/env/${package.environment}</directory>
    49. <includes>
    50. <include>**/*</include>
    51. </includes>
    52. <filtering>true</filtering>
    53. </resource>
    54. </resources>
    55. <finalName>${project.artifactId}</finalName>
    56. </build>
    57. </profile>
    58. </profiles>
    • 以上代码中 profile 就做一件事:打包的时候,默认是 dev 模式,打包 src/main/env/dev 下的配置文件,如果选择 product 则打包 src/main/env/product 下的配置文件

    IntelliJ IDEA 使用 Maven Profile 的案例

    • 在 IntelliJ IDEA 上调用 profile 简单,如下图勾选对应的复选框即可,可以多选。

    IntelliJ IDEA 配合 Maven 的一些技巧

    • 只使用 aliyun 仓库可以这样配置 settings.xml:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    5. <localRepository>D:\maven\my_local_repository</localRepository>
    6. <pluginGroups>
    7. </pluginGroups>
    8. <proxies>
    9. </proxies>
    10. <profiles>
    11. <profile>
    12. <id>aliyun</id>
    13. <repositories>
    14. <repository>
    15. <id>aliyun</id>
    16. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    17. <releases>
    18. <enabled>true</enabled>
    19. </releases>
    20. <snapshots>
    21. <enabled>true</enabled>
    22. </snapshots>
    23. </repository>
    24. </repositories>
    25. <pluginRepositories>
    26. <pluginRepository>
    27. <id>aliyun</id>
    28. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    29. <releases>
    30. <enabled>true</enabled>
    31. </releases>
    32. <snapshots>
    33. <enabled>true</enabled>
    34. </snapshots>
    35. </pluginRepository>
    36. </pluginRepositories>
    37. </profile>
    38. </profiles>
    39. <activeProfiles>
    40. <activeProfile>aliyun</activeProfile>
    41. </activeProfiles>
    42. </settings>
    • 使用 nexus + aliyun 仓库可以这样配置 settings.xml:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    5. <localRepository>D:\maven\my_local_repository</localRepository>
    6. <pluginGroups>
    7. </pluginGroups>
    8. <proxies>
    9. </proxies>
    10. <profiles>
    11. <profile>
    12. <id>nexus</id>
    13. <repositories>
    14. <repository>
    15. <id>nexus</id>
    16. <url>http://192.168.1.73:8081/repository/maven-public/</url>
    17. <releases>
    18. <enabled>true</enabled>
    19. </releases>
    20. <snapshots>
    21. <enabled>true</enabled>
    22. </snapshots>
    23. </repository>
    24. </repositories>
    25. <pluginRepositories>
    26. <pluginRepository>
    27. <id>nexus</id>
    28. <url>http://192.168.1.73:8081/repository/maven-public/</url>
    29. <releases>
    30. <enabled>true</enabled>
    31. </releases>
    32. <snapshots>
    33. <enabled>true</enabled>
    34. </snapshots>
    35. </pluginRepository>
    36. </pluginRepositories>
    37. </profile>
    38. <profile>
    39. <id>aliyun</id>
    40. <repositories>
    41. <repository>
    42. <id>aliyun</id>
    43. <url>http://maven.aliyun.com/nexus/content/groups/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>aliyun</id>
    55. <url>http://maven.aliyun.com/nexus/content/groups/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. </profiles>
    66. <activeProfiles>
    67. <activeProfile>nexus</activeProfile>
    68. </activeProfiles>
    69. </settings>