目录
4.配置私有仓库(假如所有的maven项目都使用私服,例如nexus3)
5.使用本地仓库的jar包,禁止从远端(有可能是外网的中央仓库,有可能是私服nexus3)下载
前提
下载并解压maven客户端apache-maven-3.6.3-bin.zip
1.自定义配置本地仓库目录
打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
去掉注释,并修改成:
<localRepository>D:/maven/local_repos</localRepository>
2.设置maven依赖的jdk版本
打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:
<!--
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
去掉注释,并修改成:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
3.使用指定的阿里云maven中央仓库
打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
去掉注释,并修改成:
<mirror>
<id>alimven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
4.配置私有仓库(假如所有的maven项目都使用私服,例如nexus3)
打开apache-maven-3.6.3\conf\settings.xml文件,
1.查找内容如下:
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
去掉注释,并修改成:
<mirror>
<id>nexus_release</id>
<name>nexus maven</name>
<url>http://ip地址:8081/repository/子目录/</url>
<mirrorOf>*</mirrorOf>
</mirror>
2.然后,查找内容如下:
<!--
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
去掉注释,并修改成:
<server>
<id>nexus_release</id>
<username>登录私服的用户名</username>
<password>登录私服的密码</password>
</server>
3.然后,查找内容如下:
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
去掉注释,并修改成:
<profile>
<id>nexus_profile</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://私服ip地址:8081/repository/子目录/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://私服ip地址:8081/repository/子目录/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
4.最后,查找内容如下:
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
去掉注释,并修改成:
<activeProfiles>
<activeProfile>nexus_profile</activeProfile>
</activeProfiles>
5.使用本地仓库的jar包,禁止从远端(有可能是外网的中央仓库,有可能是私服nexus3)下载
打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
去掉注释,并修改成:
<offline>true</offline>
6.将本地jar包导入本地仓库
mvn install:install-file -DgroupId=jar包的GID -DartifactId=jar包的AID -Dversion=Jar包的版本 -Dpackaging=jar -Dfile=你本地jar包的目录
例子:
mvn install:install-file -DgroupId=org.springframework -DartifactId=spring-webmvc -Dversion=3.0.5.RELEASE -Dpackaging=jar -Dfile=D:/spring.jar
7.将本地jar包导入私服nexus的maven库中
举个例子,前提是记得检查一下maven的类型是hosted类型,如果是group类型,需要进一步确定包含的hosted类型的依赖库是哪个,其中-Durl的路径是hosted类型的依赖库
mvn deploy:deploy-file -DgroupId=com.shanhy -DartifactId=shanhy-web-core -Dversion=1.1.0-SNAPSHOT -Dpackaging=jar -Dfile=shanhy-web-core-5.1.17-SNAPSHOT.jar -Durl=http://admin:admin123@127.0.0.1:8081/repository/maven-snapshots/
还可以指定文件格式是.pom文件进行导入,如果这个jar本来依赖了其他的库(可以打开jar包在META-INF最里面可以找到pom.xml文件),仅使用上面的命令,资源库中对应jar包的pom文件不会出现依赖的jar包。
-DpomFile=shanhy-web-core-5.1.17-SNAPSHOT.pom
8.将本地项目发布到Nexus私服
1.查看当前正在使用的settings.xml
mvn help:effective-settings
2.pom.xml文件中加入如下配置
<!--使用分发上传将项目打成jar包,上传到nexus私服上-->
<distributionManagement>
<!--发布版本仓库-->
<repository>
<!--nexus服务器中用户名:在settings.xml中和<server>的id一致-->
<id>releases</id>
<!--自定义名称-->
<name>RELEASES PUBLISH</name>
<!--仓库地址-->
<url>http://xx.xx.xx.xx:xxxx/repository/maven-releases/</url>
</repository>
<!--快照版本仓库-->
<snapshotRepository>
<!--nexus服务器中用户名:在settings.xml中和<server>的id一致-->
<id>snapshots</id>
<!--自定义名称-->
<name>SNAPSHOTS PUBLISH</name>
<!--仓库地址-->
<url>http://xx.xx.xx.xx:xxxx/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
3.在settings.xml文件中加入如下配置
<server>
<id>releases</id>
<username>私服的用户名</username>
<password>私服的密码</password>
</server>
<server>
<id>snapshots</id>
<username>私服的用户名</username>
<password>私服的密码</password>
</server>
4.将本地项目发布到Nexus私服
mvn clean deploy
5.发生如下错误可能是配置的账号信息有误的原因
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project demo-childA: Failed to deploy artifacts: Could not transfer artifact com.ecp:demo-childA:jar:1.0-20190625.082808-
1 from/to maven-snapshots (http://xx.xxx.xx.xx:xxxx/repository/maven-snapshots/): Failed to transfer file http://xx.xxx.xx.xx:xxxx/repository/maven-snapshots/com/ecp/demo-childA/1.0-SNAPSHOT/demo-childA-1.0-20190625.082808-1
.jar with status code 401 -> [Help 1]
可以使用:mvn help:effective-settings命令查看settings.xml配置文件查看配置信息