- 
                Notifications
    
You must be signed in to change notification settings  - Fork 20
 
Maven
目录 start
目录 end|2020-09-19 23:13|
- 下载zip包解压,将bin目录配置至PATH(最好是配置MAVEN_HOME然后引用)
 
mvn [插件]:[目标] [参数]
mvn [阶段]
- mvn archetype:generate :创建 Maven 项目
- -DgroupId=组织名/公司网址的反写+项目名
 - -DartifactId=项目名-模块名
 - -Dversion=版本号
 - -Dpackage=代码所在的包
 
 
- 
compile:编译源代码 - 
test-compile:编译测试代码 - 
test: 运行应用程序中的单元测试 - 
site: 生成项目相关信息的网站 - 
clean:清除目标目录中的生成结果 - 
package: 依据项目生成 jar 文件 - 
install:在本地 Repository 中安装 jar - 
deploy:将jar包发布到远程仓库 - 
使用id为ChatServer的Profile
-PChatServer - 
跳过测试
- 
-Dmaven.test.skip=true不执行测试用例,也不编译测试用例类。 - 
-DskipTests=true不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下 - 
-Dmaven.javadoc.skip=true跳过文档生成 
 - 
 
配置文件中配置
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>mvn install:install-file 
    -Dfile=D:\mvn\spring-context-support-3.1.0.RELEASE.jar \
    -DgroupId=org.springframework  \
    -DartifactId=spring-context-support \
    -Dversion=3.1.0.RELEASE \
    -Dpackaging=jar
要特别注意
settings.xml后者覆盖前者 加载顺序是:
maven目录/conf/setting.xml
用户目录下/.m2/setting.xml
在 用户目录下 .m2/setttings.xml 中 找到 mirrors 标签 进行添加
mirror节点即可
<mirror> 
    <id>alimaven</id> 
    <name>aliyun maven</name> 
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
    <mirrorOf>central</mirrorOf> 
</mirror> localRepository节点
pom.xml
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>反写的公司域名+项目名</groupId>
     <artifactId>项目名+模块名</artifactId>
     <!--
         第一个 0 是大版本号
         第二个 0 是分支版本号
         第三个 0 是小版本号
         snapshot 快照
         alpha 内部测试
         beta 公测
         RC 发行候选版本
         Release/GA 正式发布
     -->
     <version>0.0.1-SNAPSHOT</version>
     <!--jar war zip pom-->
     <packaging>jar</packaging>
     <!--项目描述名-->
     <name>test</name>
     <!--项目地址-->
     <url>http://maven.apache.org</url>
     <!--项目描述-->
     <description></description>
     <developers></developers>
     <licenses></licenses>
     <orgnazation></orgnazation>
    <!-- 配置属性 -->
    <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 统一定义版本 -->
        <springframework.version>1.5.6</springframework.version>
	</properties>
     <!--依赖-->
     <dependencies>
       <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <!--
            test 表明这个构件只在test目录下可以被引用
            compile 默认的
            provided 只在编译中引用
            runtime 编译和运行都有效
         -->
         <scope>test</scope>
         <!--设置依赖是否可选,默认是false-->
         <optional></optional>
         <!--排除依赖传递列表-->
         <exclusions></exclusions>
       </dependency>
     </dependencies>
     <dependencyManagement>
     </dependencyManagement>
     <build>
      <plugins>
         <!-- 构件三要素 -->
      </plugins>
     </build>
     <!--继承-->
     <parent></parent>
     <modules>
         <module></module>
     </modules>    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>父项目pom文件
    <groupId>com.github.kuangcp</groupId>
    <artifactId>Modules</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>repository</module>
        <module>service</module>
        <module>website</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>子项目pom文件
    <artifactId>website</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.github.kuangcp</groupId>
        <artifactId>Modules</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>简单配置
    <profiles>
        <profile>
            <id>development</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties></properties>
        </profile>
        <profile>
            <id>production</id>
            <properties></properties>
        </profile>
    </profiles>Maven 如何为不同的环境打包
开发、测试和产品环境
- 
使用 test profile 执行命令
mvn clean package -P test - 
子项目编译打包各自独立,怎么整合成一个
 
mvn test
- 跳过测试 
mvn test -DskipTests - 执行指定测试类 
mvn test -Dtest=类名 
<plugin>
    <artifactId>maven-source-plugin</artifactId>
    <executions>
        <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
                <goal>jar-no-fork</goal>
            </goals>
        </execution>
    </executions>
</plugin>不依赖Jar的项目
依赖Jar的项目
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.xxx.Main</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.xxx.Main</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>war和jar一样使用
- Springboot项目能够做到, 其实就是 Main 方法, 然后配置了一个Servlet的加载类就可以当war用了
 
依赖范围就是用来控制依赖和三种classpath(编译classpath,测试classpath、运行classpath)的关系
- 
compile:编译依赖范围。如果没有指定,就会默认使用该依赖范围。使用此依赖范围的Maven依赖,对于编译、测试、运行三种classpath都有效。- 典型的例子是spring-core,在编译、测试和运行的时候都需要使用该依赖。
 
 - 
test: 测试依赖范围。使用次依赖范围的Maven依赖,只对于测试classpath有效- 在编译主代码或者运行项目的使用时将无法使用此依赖。典型的例子是Jnuit,它只有在编译测试代码及运行测试的时候才需要。
 
 - 
provided:已提供依赖范围。使用此依赖范围的Maven依赖,对于编译和测试classpath有效,但在运行时候无效。- 典型的例子是servlet-api,编译和测试项目的时候需要该依赖,但在运行项目的时候,由于容器以及提供,就不需要Maven重复地引入一遍。
 
 - 
runtime:运行时依赖范围。使用此依赖范围的Maven依赖,对于测试和运行classpath有效,但在编译主代码时无效。- 典型的例子是JDBC驱动实现,项目主代码的编译只需要JDK提供的JDBC接口,只有在执行测试或者运行项目的时候才需要实现上述接口的具体JDBC驱动。
 
 
| 依赖范围 Scope | 对于编译classpath | 对于测试classpath | 对于运行classpath | Demo | 
|---|---|---|---|---|
| compile | Y | Y | Y | spring-boot-starter-web | 
| test | Y | Junit | ||
| provided | Y | Y | servlet-api | |
| runtime | Y | Y | JDBC的实现Jar | |
| system | Y | Y | Maven仓库之外的类库文件 | 
- 比如一个account-email项目为例
- account-email有一个compile范围的spring-code依赖,
 - spring-core有一个compile范围的commons-logging依赖,
 
 - 那么commons-logging就会成为account-email的compile的范围依赖,commons-logging是account-email的一个传递性依赖
 
项目A依赖B
A项目 pom.xml中配置依赖 (构件三要素)
B项目 先clean package
      然后build 的 install
A 项目 compile
- 依赖路径短优先
- 1 A->B->C->X(jar文件)
 - 2 A->C->X(jar文件)
 - 会选择 2 中的X的jar版本
 
 - 先声明的优先
 
对应的<dependency>标签中添加
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>新建一个项目作为父项目
然后在需要引用父项目的子项目pom文件中, 加上parent 标签里面写上 父项目的三要素
maven 插件
本质还是要使用系统安装的protoc, 然后插件实现了自动编译文件
- 
protoc-jar
自动识别平台, 使用对应的编译器编译得到java文件, 但是目前还有一些bug 
Maven Enforcer Plugin - Baeldung
跳过 deploy
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>3.0.0-M1</version>
        <configuration>
            <skip>true</skip>
        </configuration>
    </plugin>码农翻身:小李的Build之路(上) | 码农翻身:小李的Build之路(下)
这个小故事讲述了ant到maven的演化
- 
ant脚本是可以直接运行在maven中的。maven和ant最大的差别就是在于maven的编译以及所有的脚本都有一个基础,就是POM(project object model)。这个模型定义了项目的方方面面,然后各式各样的脚本在这个模型上工作,而ant完全是自己定义,显然maven更胜一筹。
 - 
maven对所依赖的包有明确的定义,如使用那个包,版本是多少,一目了然。而ant则通常是简单的inclde 所有的jar。导致的最终结果就是,你根本无法确定JBoss中的lib下的common-logging 是哪个版本的,唯一的方法就是打开 META-INF 目录下MANIFEST.MF。
 - 
maven是基于中央仓库的编译,即把编译所需要的资源放在一个中央仓库里,如jar,tld,pom,等。当编译的时候,maven会自动在仓库中找到相应的包,如果本地仓库没有,则从设定好的远程仓库中下载到本地。这一切都是自动的,而ant需要自己定义了。这个好处导致的结果就是,用maven编译的项目在发布的时候只需要发布源码,小得很,而反之,ant的发布则要把所有的包一起发布,显然maven又胜了一筹。
 - 
maven有大量的重用脚本可以利用,如生成网站,生成javadoc,sourcecode reference,等。而ant都需要自己去写。
 - 
maven目前不足的地方就是没有象ant那样成熟的GUI界面,不过mavengui正在努力中。目前使用maven最好的方法还是命令行,又快又方便
 
- 协同开发的基本规范,为大家提供方便的协作的模式,能增加代码的复用,提高生产率。
 - 提供方便,规范化的打包方法,是公司完成自动构建系统的核心部分,能帮助提高敏捷开发的效率(敏捷开发提倡尽早集成)。
 - 减少冗余,减少出错的可能。
 - 中心资源库管理,能减低源码库的大小,中心资源库可以统一定期备份。
 - 目录结构规范,让开发者从一个maven项目过度到另一maven项目很容易。
 - 大量的开源项目使用了maven。
 
- Jforg mvnrepository
 
不用去跑审核流程, 私有, 快速, 便捷
需要运行软件, 一般公司内部局域网使用, 如果自己有服务器也能开放给公众使用 参考: maven私服搭建及gradle上传
- 设置 RELEASE 不可重复 deploy 管理后台 Repositories -> Releases -> ANALYZE -> Configuration -> Deployment Policy 设置为 Disable Redeploy
 
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <url>http://192.168.0.221:8081/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>http://192.168.0.221:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>利用公开仓库来搭建私服 | 参考: 使用git仓库搭建maven私服
当然了在各个托管平台都可以的, 只不过码云是国内的, 毕竟要快 github gitlab bitbucket 就....
- 创建好一个空的公开仓库
 - 使用统一的groupId, 这样就会放到默认目录 
~/.m2/repository/下 只要在groupId对应的目录下 git init - 只需在项目中执行install, 然后在此目录进行提交即可
 
build.gradle 中添加
repositories {
    maven{
        url "https://gitee.com/你的用户名/创建的仓库/raw/master"
    }
}pom.xml中添加
    <repositories>
    <repository>
        <id>mvnrepo</id>
        <name>mvn repository</name>
        <url>https://gitee.com/用户名/仓库/raw/master</url>
    </repository>
    </repositories>- 
【 Algorithm 】
 - 
【 Blog 】
 - 
【 C 】
 - 
【 Database 】
 - 
【 Distributed 】
 - 
【 FrontEnd 】
- 【 FrontEnd/Frame 】
 - 【 FrontEnd/Node 】
 - Font
 - Hexo
 - JavaScript
 - LearnPS
 - ResponseCode
 - SVG
 - ViewSolution
 - extjs学习笔记
 
 - 
【 Functional 】
 - 
【 Go 】
 - 
【 Groovy 】
 - 
【 Java 】
- 【 Java/AdvancedLearning 】
- 【 JavaBasic 】
 - 【 JavaCache 】
 - 【 JavaCollection 】
 - 【 JavaConcurrency 】
 - 【 JavaMap 】
 - Annotation
 - ClassFile
 - Collection
 - Concurrency
 - Deploy
 - Exception
 - ExtendsAndInterface
 - Generics
 - IO
 - JDBC
 - JDKAndJRE
 - JMX
 - JVM
 - Java11
 - Java7
 - Java8
 - JavaNetwork
 - JavaReleaseVersion
 - JavaWeb
 - JvmPerformance
 - MQ
 - MultipleLanguage
 - Proxy
 - Reflection
 - Serialize
 - SyntaxAndType
 - Thread
 - WebPerformance
 
 - 【 Java/Android 】
 - 【 Java/Ecosystem 】
 - 【 Java/MSA 】
 - 【 Java/Spring 】
 - 【 Java/TemplateEngine 】
 - 【 Java/Test 】
 - 【 Java/Tool 】
 - 【 Java/thread 】
 - AlibabaJavaStandard
 - DesignPattern
 - HashMap解析
 - Java-NIO
 - Java虚拟机
 - Log
 - MIS
 - Quartz
 - RESTful
 - WebSocket学习笔记
 - ZooKeeper学习笔记
 - android学习笔记
 
 - 【 Java/AdvancedLearning 】
 - 
【 Kotlin 】
 - 
【 Linux 】
- 【 Linux/Alpine 】
 - 【 Linux/Arch 】
 - 【 Linux/Base 】
 - 【 Linux/Centos 】
 - 【 Linux/Container 】
 - 【 Linux/Debian 】
 - 【 Linux/Tool 】
 - JavaDevInit
 - Linux系统学习
 
 - 
【 MyBlog 】
 - 
【 Python 】
- 【 Python/Tool 】
 - Python
 - PythonConcurrent
 - PythonGUI
 - PythonGame
 - PythonNet
 - PythonOffices
 - PythonWeb
 - Python基础
 - Python核心学习
 
 - 
【 Reactive 】
 - 
【 Rust 】
 - 
【 Scala 】
 - 
【 Script 】
 - 
【 Skills 】
- 【 Skills/Application 】
 - 【 Skills/CS 】
 - 【 Skills/Cache 】
 - 【 Skills/Councurrency 】
 - 【 Skills/DevOps 】
 - 【 Skills/Document 】
 - 【 Skills/Ecology 】
 - 【 Skills/Network 】
 - 【 Skills/Search 】
 - 【 Skills/SoftwareEngineering 】
 - 【 Skills/Spider 】
 - 【 Skills/Test 】
 - 【 Skills/Vcs 】
 - 【 Skills/Work 】
 - AppManual
 - CelebrityQuotes
 - Miscellaneous
 - Platform
 - Problem
 - Protobuf
 - RegularExpression
 - SoftwareDesignEngineer
 - Website
 
 - 
【 Windows 】