Maven

2 篇文章 0 订阅

Maven基础

下载、安装

下载后解压:apache-maven-3.6.3
配置环境变量:
系统变量:M2_HOME/D:\apache-maven-3.6.3
PATH变量:%M2_HOME%\bin;

mvn -version

显示版本,路径等相关信息

linux 安装

解压

[root@iZbp14eey5dlgt99xx7qptZ data]# tar -zxvf apache-maven-3.6.3-bin.tar.gz -C /usr/local/

配置环境变量

[root@iZbp14eey5dlgt99xx7qptZ apache-maven-3.6.3]# vim /etc/profile //export PATH=$PATH:/usr/local/apache-maven-3.6.3/bin
source /etc/profile //刷新环境变量

测试

[root@iZbp14eey5dlgt99xx7qptZ /]# mvn -version

仓库

本地仓库:本地的包目录,C:\Users\Administrator.m2\repository
远程仓库:
中央仓库:maven官方包目录,maven的安装目录下
D:\apache-maven-3.3.9\lib\maven-model-builder-3.3.9.jar\pom-4.0.0.xml\repositories节点
私服:公司内部搭建
公共仓库:阿里云、开源中国是中央仓库的镜像

下载jar包顺序:本地/私服/中央

修改仓库路径

修改本地仓库路径
在这里插入图片描述
mirror镜像

	 	<mirror> 
		<id>alimaven</id> 
		<name>aliyun maven</name> 
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
		<mirrorOf>central</mirrorOf> 
		</mirror>  
// 配置私服--发布到私服
// settings.xml
	 <server>  
          <id>testnew</id>  
          <username>admin</username>  
          <password>123</password>  
        </server>
// pom.xml
<distributionManagement>
                <repository>
                        <id>testnew</id>
                        <name>testnew-releases</name>
                        <url>http://maven.test/</url>
                </repository>
                <snapshotRepository>
                        <id>testnew</id>
                        <name>testnew-snapshots</name>
                        <url>http://maven.test/</url>
                </snapshotRepository>
        </distributionManagement>

//直接修改镜像地址为公司私服地址

	<mirror> 
		<id>companytest</id> 
		<name>companytest-releases</name> 
		<url>http://maven.test</url> 
		<mirrorOf>central</mirrorOf> 
	</mirror>

生命周期、插件

插件:不同的插件用于不同的功能
生命周期:将不同的插件按照顺序封装,来打包不同阶段的功能

打包

3种打包方式
1.非maven项目,直接将web目录复制到tomcat下
不推荐,将会丢失引用的第三方包
2.非maven项目,直接用工具export成war包,并放到tomcat目录下(推荐)
3.maven 项目
打包那种格式的,根据需要在pom.xml配置

jar、war

jar包就是别人已经写好的一些类,然后对这些类进行打包。可以将这些jar包引入到你的项目中,可以直接使用这些jar包中的类和属性,这些jar包一般放在lib目录下

war是一个可以直接运行的web模块,通常用于网站,打成包部署到容器中。以Tomcat来说,将war包放置在其\webapps\目录下,然后启动Tomcat,这个包就会自动解压,就相当于发布了。

可执行jar包:
补充节点:http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#ManifestResourceTransformer

META-INF/MANIFEST.MF/Main-Class 配置类名后,则为可执行包

使用maven自动配置此类名:

  <build>
	<plugins>
		<plugin>
		        <groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>3.2.4</version>
		 <executions>
			<execution>
				<phase>package</phase>
				<goals>
					<goal>shade</goal>
				</goals>
				<configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>com.bhzt.maven.Animal</Main-Class>
                    <X-Compile-Source-JDK>1.8</X-Compile-Source-JDK>
                    <X-Compile-Target-JDK>1.8</X-Compile-Target-JDK>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
			</execution>
		 </executions>
		</plugin>
	</plugins>
  </build>
package com.bhzt.maven;

public class Animal {
    public static void shout(){
        System.out.println("动物如何叫");
    }

    public  static  void main(String[] args)
    {
        shout();
    }
}

在这里插入图片描述

MAVEN 命令

dos命令:
cls:清屏
dir:展示目录文件
cd /d e:\:切换磁盘
https://mvnrepository.com/search?q=maven-archetype-plugin
搜索第三方包;
打开需要版本;
view all;

help:describe -Dplugin

获取某个插件的功能描述:
mvn help:describe -Dplugin -Dplugin=groupId:artifactId:version
简写:mvn help:describe -Dplugin -Dplugin=archetype

在这里插入图片描述
该插件有7个方法,并分别列出

maven-archetype-plugin 生成项目骨架

在这里插入图片描述
在这里插入图片描述

eclipse插件,用于生成eclipse打开所必须的文件

//查看插件使用描述
mvn help:describe -Dplugin -Dplugin=eclipse

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

idea插件,用于生成idea打开所必须的文件

//查看插件使用描述
mvn help:describe -Dplugin -Dplugin=idea
mvn idea:idea //生成
mvn idea:clean//清除

compiler 编译

//查看插件使用描述
mvn help:describe -Dplugin -Dplugin=compiler
mvn compiler:compile //编译
mvn compiler:testCompile //编译-测试包

pom.xml 增加依赖包

在这里插入图片描述

surefire插件,测试

使用此插件时需要先编译,这就是插件的灵活性

shade插件,打包第三方jar包

//查看插件使用描述
mvn help:describe -Dplugin -Dplugin=shade

1.在pom.xml中声明

    <build>
	<plugins>
		<plugin>
		        <groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>3.2.4</version>
		 <executions>
			<execution>
				<phase>package</phase>
				<goals>
					<goal>shade</goal>
				</goals>
			</execution>
		 </executions>
		</plugin>
	</plugins>
  </build>
mvn package //将会打两个包,一个包含第三方的包

maven 生命周期使用

1.mvn compile :编译
2.mvn test:编译测试包,会自动执行步骤1
3.mvn package:会自动执行前两步

Nexus 私服

安装

1.解压

[root@iZbp14eey5dlgt99xx7qptZ data]# tar -zxvf nexus-3.24.0-02-unix.tar.gz 

2.移动
[root@iZbp14eey5dlgt99xx7qptZ data]# mv nexus-3.24.0-02 /usr/local/nexus-3.24.0-02

3.启动

[root@iZbp14eey5dlgt99xx7qptZ bin]# pwd
/usr/local/nexus/nexus-3.24.0-02/bin
[root@iZbp14eey5dlgt99xx7qptZ bin]# ls
contrib  nexus  nexus.rc  nexus.vmoptions
[root@iZbp14eey5dlgt99xx7qptZ bin]# ./nexus
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}
[root@iZbp14eey5dlgt99xx7qptZ bin]# 

//新增用户
[root@iZbp14eey5dlgt99xx7qptZ bin]# sudo  adduser -m puhome -s/bin/bash sudo passwd Z******24

//对puhome用户授权
chown nexus:puhome -R sonatype-work/

//切换用户 jdk环境变量失效
su puhome

//切换用户后 jdk环境变量失效,root用户也失效
source /etc/profile //两个用户都有了

//列出可执行的命令
./nexus

//启动
[puhome@iZbp14eey5dlgt99xx7qptZ bin]$ pwd
/usr/local/nexus/nexus-3.24.0-02/bin
[puhome@iZbp14eey5dlgt99xx7qptZ bin]$ ./nexus start
Starting nexus
[puhome@iZbp14eey5dlgt99xx7qptZ bin]$


//启动失败
./nexus run
//报错--必须得2G内存(1G内存无法启动)
nexus Cannot allocate memory

//启动成功
[puhome@iZbp14eey5dlgt99xx7qptZ bin]$ ./nexus start
Starting nexus
[puhome@iZbp14eey5dlgt99xx7qptZ bin]$ ./nexus staus
Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}
[puhome@iZbp14eey5dlgt99xx7qptZ bin]$ 


//成功的进程状态
[puhome@iZbp14eey5dlgt99xx7qptZ bin]$ ps -ef | grep nexus
puhome      2045       1 13 19:09 pts/0    00:01:04 /usr/local/java/jdk1.8.0_251/bin/java -server -Dinstall4j.jvmDir=/usr/local/java/jdk1.8.0_251 -Dexe4j.moduleName=//usr/local/nexus/nexus-3.13.0-01/bin/nexus -XX:+UnlockDiagnosticVMOptions -Dinstall4j.launcherId=245 -Dinstall4j.swt=false -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Xms1200M -Xmx1200M -XX:MaxDirectMemorySize=2G -XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass -XX:+LogVMOutput -XX:LogFile=../sonatype-work/nexus3/log/jvm.log -XX:-OmitStackTraceInFastThrow -Djava.net.preferIPv4Stack=true -Dkaraf.home=. -Dkaraf.base=. -Dkaraf.etc=etc/karaf -Djava.util.logging.config.file=etc/karaf/java.util.logging.properties -Dkaraf.data=../sonatype-work/nexus3 -Djava.io.tmpdir=../sonatype-work/nexus3/tmp -Dkaraf.startLocalConsole=false -Di4j.vpt=true -classpath //usr/local/nexus/nexus-3.13.0-01/.install4j/i4jruntime.jar://usr/local/nexus/nexus-3.13.0-01/lib/boot/nexus-main.jar://usr/local/nexus/nexus-3.13.0-01/lib/boot/org.apache.karaf.main-4.0.9.jar://usr/local/nexus/nexus-3.13.0-01/lib/boot/org.osgi.core-6.0.0.jar://usr/local/nexus/nexus-3.13.0-01/lib/boot/org.apache.karaf.diagnostic.boot-4.0.9.jar://usr/local/nexus/nexus-3.13.0-01/lib/boot/org.apache.karaf.jaas.boot-4.0.9.jar com.install4j.runtime.launcher.UnixLauncher start 9d17dc87   org.sonatype.nexus.karaf.NexusMain
puhome      2674    1342  0 19:17 pts/0    00:00:00 grep --color=auto nexus

基础

Maven-Central:中央仓库,类型为proxy 代理仓库,代理的是Maven官方的仓库
Maven-release:jar包的发行版,hosted为本地的
Maven-snapshops:jar包的快照版
Maven-public:提供了类似聚合的功能,可以访问上述所有的Maven仓库,访问它相当于同时访问该组下面的所有仓库
,通过移除某个仓库,来控制public范围

repositories维护:

在这里插入图片描述

将新增的aliyun代理,加到public组里

在这里插入图片描述

禁用匿名账号

在这里插入图片描述

将releases和snapshots设置为可重复提交

在这里插入图片描述

远程仓库下载配置方式

通过mirror配置

	  //用来进行对mirrors和profiles进行身份认证
	 //id要和mirrors或profiles中的id进行对应
	 //开始讲server的username节点名称复制错了 导致下载时提示用户未授权
	 <server>
      <id>puhome2020</id>
      <username>admin</username>
      <password>Z********4</password>
    </server>
	
	//通过镜像地址下载相应的依赖 
	//mirrorOf哪些操作的类型通过此镜像进行下载,*,表示所有的请求都通过此镜像下载,分central/release等
		<mirror> 
		<id>puhome2020</id> 
		<name>puhome maven</name> 
		<url>http://116.62.111.129:8081/repository/maven-public/</url> 
		<mirrorOf>*</mirrorOf> 
		</mirror>
	//新建项目后 没有生效,原因是默认仓库地址还是走的C盘目录,需要调整到修改settings.xml的目录

通过profile配置

与mirror区别,当私服挂了,自动连接远程仓库(如果配了镜像,则先从镜像地址下载)


	<server>
      <id>puhome2020</id>
      <username>admin</username>
      <password>Z********4</password>
    </server>
	
	//特别注意,由于</repository>前面有特殊空格导致未起作用,见下图
	<profiles>
	<profile>     
              <id>nexuspuhome</id>
              <repositories>
                    <repository>     
                       <id>puhome2020</id>     
                       <name>puhome maven</name>     
                       <url>http://116.62.111.129:8081/repository/maven-public/</url>     
                       <releases>     
                            <enabled>true</enabled>     
                       </releases>     
                       <snapshots>     
                            <enabled>false</enabled>     
                       </snapshots>    
                   </repository>
              </repositories> 
           </profile>
  </profiles>
  
<activeProfiles>
    <activeProfile>nexuspuhome</activeProfile>
</activeProfiles>

在这里插入图片描述

上传私服

pox.xml添加节点

//SNAPSHOT版本
<version>1.0-SNAPSHOT</version>
//RELEASE版本
<version>1.0-RELEASE</version>

<!--    上传的仓库地址-->
    <distributionManagement>
        <repository>
<!--            与settings.xml中的server的id对应,上传需要身份认证-->
            <id>puhome2020</id>
            <name>releases</name>
<!--            稳定版 通过识别上面节点version来自动匹配上传的私服地址-->
            <url>http://116.62.111.129:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>puhome2020</id>
            <name>snapshots</name>
<!--            开发版-->
            <url>http://116.62.111.129:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
演示
clean:清除target目录
validate:验证项目所必要信息是否可用
compile:编译项目的源代码
test:编译测试源代码
package:打包
verify:对集成测试结果执行任何检查
install:将远程仓库软件包安装到本地存储库中,用做本地其它项目的依赖项
site:
deploy:将包上传到远程仓库
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值