Maven 之setting.xml pom.xml

1)、 配置Maven 从私服上下载构件

a、在POM.xml文件中配置

  1. <repositories>  
  2.     <repository>  
  3.         <id>central.maven.com</id>  
  4.         <name>mapbar central mirror.</name>  
  5.         <url>http://192.168.1.252:8081/nexus/content/repositories/central/</url>  
  6.     </repository>  
  7.     <repository>  
  8.         <id>3rd.mapbar.com</id>  
  9.         <name>mapbar thirdparty central mirror.</name>  
  10.         <url>http://192.168.1.252:8081/nexus/content/repositories/thirdparty/</url>  
  11.     </repository>  
  12.     <repository>  
  13.         <id>public.mapbar.com</id>  
  14.         <name>mapbar tech API maven mirror.</name>  
  15.         <url>http://192.168.1.252:8081/nexus/content/groups/public/</url>  
  16.     </repository>  
  17.     <repository>  
  18.         <id>releases.mapbar.com</id>  
  19.         <name>mapbar thirdparty central mirror.</name>  
  20.         <url>http://192.168.1.252:8081/nexus/content/repositories/releases/</url>  
  21.     </repository>  
  22. </repositories>  

这样的配置只对Maven项目有效,在实际引用中,我们想通过一次配置就能让本机所有的Maven项目都能使用自己的Maven私服。这个时候就要用settings.xml 文件。改文件对本机所有的Maven项目有效。配置如下:

  1. <profiles>  
  2.     <profile>  
  3.         <id>nexus</id>  
  4.         <repositories>  
  5.             <repository>  
  6.                 <id>nexus</id>  
  7.                 <name>Nexus</name>  
  8.                 <url>http://192.168.53.55:8081/nexus/content/groups/public/</url>  
  9.                 <releases>  
  10.                     <enabled>true</enabled>  
  11.                 </releases>  
  12.                 <snapshots>  
  13.                     <enabled>true</enabled>  
  14.                 </snapshots>  
  15.             </repository>  
  16.         </repositories>  
  17.         <pluginRepositories>  
  18.             <pluginRepository>  
  19.                 <id>nexus</id>  
  20.                 <name>Nexus</name>  
  21.                 <url>http://192.168.53.55:8081/nexus/content/groups/public/</url>  
  22.                 <releases>  
  23.                     <enabled>true</enabled>  
  24.                 </releases>  
  25.                 <snapshots>  
  26.                     <enabled>true</enabled>  
  27.                 </snapshots>  
  28.             </pluginRepository>  
  29.         </pluginRepositories>  
  30.     </profile>  
  31. </profiles>  
  32. <activeProfiles>  
  33.     <activeProfile>nexus</activeProfile>  
  34. </activeProfiles>  


2)、配置自动发布构件到私服

POM.XML 配置:

  1. <distributionManagement>  
  2.       <repository>  
  3.         <id>releases</id>  
  4.         <url>http://localhost:8081/nexus/content/repositories/thirdparty/</url>  
  5.       </repository>  
  6. </distributionManagement>  

在命令行键入:mavn  deploy 则构件自动发布到本地和上传到私服 http://localhost:8081/nexus/content/repositories/thirdparty 这个目录下


需要注意2点:

1、发布的版本类型必须和nexus里的Policy类型一致。

2、setting.xml 文件必须配置servers,其中id必须和repository下的id一致。

  1. <servers>  
  2.         <server>  
  3.             <id>releases</id>  
  4.             <username>admin</username>  
  5.             <password>admin123</password>  
  6.         </server>  
  7.         <server>  
  8.             <id>snapshots</id>  
  9.             <username>admin</username>  
  10.             <password>admin123</password>  
  11.         </server>  
  12.         <server>  
  13.             <id>deploymentRepo</id>  
  14.             <username>admin</username>  
  15.             <password>admin123</password>  
  16.         </server>  
  17.     </servers>  



3)、在<build> 中配置项目构建信息,生成doc,source  

  1. <!-- 项目的构建信息 -->  
  2.     <build>  
  3.             <plugins>  
  4.             <!-- generate a javasource -->  
  5.             <plugin>  
  6.                 <groupId>org.apache.maven.plugins</groupId>  
  7.                 <artifactId>maven-source-plugin</artifactId>  
  8.                 <executions>  
  9.                     <execution>  
  10.                         <id>attach-sources</id>  
  11.                         <goals>  
  12.                             <goal>jar</goal>  
  13.                         </goals>  
  14.                     </execution>  
  15.                 </executions>  
  16.             </plugin>  
  17.             <!-- generate a javadoc -->  
  18.             <plugin>  
  19.                 <groupId>org.apache.maven.plugins</groupId>  
  20.                 <artifactId>maven-javadoc-plugin</artifactId>  
  21.                 <executions>  
  22.                     <execution>  
  23.                         <id>attach-javadocs</id>  
  24.                         <goals>  
  25.                             <goal>jar</goal>  
  26.                         </goals>  
  27.                     </execution>  
  28.                 </executions>  
  29.             </plugin>  
  30.         </plugins>  
  31.     </build>  


输入命令:mvn install

 在target目录下生成了

MavenTest-0.0.1-release.jar           --.class文件

MavenTest-0.0.1-release-sources.jar    --.java 文件

MavenTest-0.0.1-release-javadoc.jar   --doc 文件

配置 Maven 的 `settings.xml` 文件是优化构建流程、设置代理仓库自定义行为的重要步骤。以下是关于如何正确配置 `settings.xml` 的详细说明。 ### 配置文件的位置 Maven 支持两种 `settings.xml` 文件: - **全局配置**:位于 Maven 安装目录下的 `conf/settings.xml`,适用于所有用户。 - **用户配置**:位于用户主目录下的 `.m2/settings.xml`,仅适用于当前用户[^2]。 通常推荐使用用户级别的 `settings.xml` 文件,因为它不会影响其他用户,并且可以更灵活地进行个性化配置。 --- ### 常见配置项说明 #### 1. 镜像配置(Mirrors) 镜像用于替换默认的中央仓库地址,常用于加速依赖下载或使用私有仓库。以下是一个配置国内镜像的例子: ```xml <mirrors> <mirror> <id>maven-net-cn</id> <name>Maven China Mirror</name> <url>http://maven.net.cn/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> ``` 此配置将 Maven 默认的中央仓库替换为国内的镜像地址,从而加快依赖下载速度[^4]。 #### 2. 服务器认证信息(Servers) 如果你使用了私有仓库,需要在 `servers` 节点中配置认证信息: ```xml <servers> <server> <id>my-repo</id> <username>your-username</username> <password>your-password</password> </server> </servers> ``` 确保 `id` 与 `pom.xml` 或 `profiles.xml` 中引用的仓库 ID 一致。 #### 3. 仓库配置(Profiles) `profiles` 是用于定义不同环境配置的主要部分。例如,可以定义 JDK 版本、仓库地址等: ```xml <profiles> <profile> <id>default-jdk-11</id> <activation> <activeByDefault>true</activeByDefault> <jdk>11</jdk> </activation> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> </profile> </profiles> ``` 此配置会自动激活并使用 JDK 11 进行编译[^3]。 #### 4. 环境变量配置(Properties) 可以在 `properties` 节点中定义一些键值对,供其他插件或模块使用: ```xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> ``` --- ### 在 IDE 中配置 settings.xml 如果你使用的是 Eclipse 或 IntelliJ IDEA,需要手动指定使用的 `settings.xml` 文件路径: - **Eclipse**:进入 `Window > Preferences > Maven > User Settings`,选择本地的 `settings.xml` 文件。 - **IntelliJ IDEA**:在设置中找到 Maven 配置,指定 `User settings file` `Local repository` 的位置[^5]。 --- ### 注意事项 - 修改完 `settings.xml` 后,建议清理本地仓库缓存(删除 `.m2/repository` 目录)以避免旧配置残留问题。 - 多人协作项目中,应统一 `settings.xml` 的关键配置(如镜像、仓库、认证信息),以避免构建不一致问题。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值