Maven【入门笔记】

Maven 解决版本依赖的问题

https://www.liaoxuefeng.com/wiki/1252599548343744/1309301146648610

如果没有项目管理工具,在开发项目的时候,我们需要手动管理依赖包,需要管理依赖包的版本、去找到并下载依赖包、还有依赖包所依赖的包 等等。会非常的麻烦且容易出问题,而Maven就是解决这个麻烦的。
首先,我们需要确定引入哪些依赖包。例如,如果我们需要用到commons logging,我们就必须把commons logging的jar包放入classpath。如果我们还需要log4j,就需要把log4j相关的jar包都放到classpath中。这些就是依赖包的管理。
其次,我们必须

Maven就是是专门为Java项目打造的管理和构建工具,它的主要功能有:

  • 提供了一套标准化的项目结构;
  • 提供了一套标准化的构建流程(编译,测试,打包,发布……);-
  • 提供了一套依赖管理机制。
  • 构建
  • 文档生成
  • 报告
  • 依赖
  • SCMs
  • 发布
  • 分发
  • 邮件列表

Maven 项目结构

a-maven-project
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ └── resources
│ └── test
│ ├── java
│ └── resources
└── target

项目的根目录a-maven-project是项目名,它有一个项目描述文件pom.xml,存放Java源码的目录是src/main/java,存放资源文件的目录是src/main/resources,存放测试源码的目录是src/test/java,存放测试资源的目录是src/test/resources,最后,所有编译、打包生成的文件都放在target目录里。这些就是一个Maven项目的标准目录结构。

项目 模块化
Maven【入门笔记】_第1张图片
模块的pom,需要设置父pom信息

    
        com.itranswarp.learnjava
        parent
        1.0
        ../parent/pom.xml
    
    
    module-a
    jar
    module-a

父pom需要设置module信息


    <modules>
        <module>parent</module>
        <module>module-a</module>
        <module>module-b</module>
        <module>module-c</module>
    </modules>

Maven 仓库管理

Maven中央仓库:repo1.maven.org,有Maven社区管理的。
Maven本地仓库:用户主目录的.m2目录。可在setting.xml文件中的localRepository配置指定
私有仓库:公司内部如果不希望把源码和jar包放到公网上,可以创建私有仓库,使用方式和中央仓位没有任何区别。
Maven镜像仓库:如阿里云提供的Maven镜像仓库 https://maven.aliyun.com/repository/central

Maven如何知道从何处下载所需的依赖?也就是相关的jar包?答案是Maven维护了一个中央仓库(repo1.maven.org),所有第三方库将自身的jar以及相关信息上传至中央仓库,Maven就可以从中央仓库把所需依赖下载到本地。
Maven并不会每次都从中央仓库下载jar包。一个jar包一旦被下载过,就会被Maven自动缓存在本地目录(用户主目录的.m2目录),所以,除了第一次编译时因为下载需要时间会比较慢,后续过程因为有本地缓存,并不会重复下载相同的jar包。
Maven 依赖搜索顺序
当我们执行 Maven 构建命令时,Maven 开始按照以下顺序查找依赖的库:

步骤 1 - 在本地仓库中搜索,如果找不到,执行步骤 2,如果找到了则执行其他操作。
步骤 2 - 在中央仓库中搜索,如果找不到,并且有一个或多个远程仓库已经设置,则执行步骤 4,如果找到了则下载到本地仓库中以备将来引用。
步骤 3 - 如果远程仓库没有被设置,Maven 将简单的停滞处理并抛出错误(无法找到依赖的文件)。
步骤 4 - 在一个或多个远程仓库中搜索依赖的文件,如果找到则下载到本地仓库以备将来引用,否则 Maven 将停止处理并抛出错误(无法找到依赖的文件)。

Maven【入门笔记】_第2张图片
Maven【入门笔记】_第3张图片

Maven 配置

环境配置:https://www.runoob.com/maven/maven-setup.html

Maven 常用命令

Maven的生命周期(lifecycle):default、clean

Maven的生命周期由一系列阶段(phase)构成,以内置的生命周期default为例,它包含以下phase:
validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
prepare-package
package
pre-integration-test
integration-test
post-integration-test
verify
install
deploy

Maven另一个常用的生命周期是clean,它会执行3个phase:
pre-clean
clean (注意这个clean不是lifecycle而是phase)
post-clean

在实际开发过程中,经常使用的命令有:
mvn clean:清理所有生成的class和jar;
mvn clean compile:先清理,再执行到compile;
mvn clean test:先清理,再执行到test,因为执行test前必须执行compile,所以这里不必指定compile;
mvn clean package:先清理,再执行到package。
Maven【入门笔记】_第4张图片

Maven【入门笔记】_第5张图片

Maven 构建生命周期

详细:https://www.runoob.com/maven/maven-build-life-cycle.html
Maven【入门笔记】_第6张图片
Maven【入门笔记】_第7张图片

POM标签大全

来自:https://www.runoob.com/maven/maven-pom.html

<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.0http://maven.apache.org/maven-v4_0_0.xsd">
    
    <parent>
        
        <artifactId />
        
        <groupId />
        
        <version />
        
        <relativePath />
    parent>
    
    <modelVersion>4.0.0modelVersion>
    
    <groupId>asia.banseongroupId>
    
    <artifactId>banseon-maven2artifactId>
    
    <packaging>jarpackaging>
    
    <version>1.0-SNAPSHOTversion>
    
    <name>banseon-mavenname>
    
    <url>http://www.baidu.com/banseonurl>
    
    <description>A maven project to study maven.description>
    
    <prerequisites>
        
        <maven />
    prerequisites>
    
    <issueManagement>
        
        <system>jirasystem>
        
        <url>http://jira.baidu.com/banseonurl>
    issueManagement>
    
    <ciManagement>
        
        <system />
        
        <url />
        
        <notifiers>
            
            <notifier>
                
                <type />
                
                <sendOnError />
                
                <sendOnFailure />
                
                <sendOnSuccess />
                
                <sendOnWarning />
                
                <address />
                
                <configuration />
            notifier>
        notifiers>
    ciManagement>
    
    <inceptionYear />
    
    <mailingLists>
        
        <mailingList>
            
            <name>Demoname>
            
            <post>[email protected]post>
            
            <subscribe>[email protected]subscribe>
            
            <unsubscribe>[email protected]unsubscribe>
            
            <archive>http:/hi.baidu.com/banseon/demo/dev/archive>
        mailingList>
    mailingLists>
    
    <developers>
        
        <developer>
            
            <id>HELLO WORLDid>
            
            <name>banseonname>
            
            <email>[email protected]email>
            
            <url />
            
            <roles>
                <role>Project Managerrole>
                <role>Architectrole>
            roles>
            
            <organization>demoorganization>
            
            <organizationUrl>http://hi.baidu.com/banseonorganizationUrl>
            
            <properties>
                <dept>Nodept>
            properties>
            
            <timezone>-5timezone>
        developer>
    developers>
    
    <contributors>
        
        <contributor>
            <name />
            <email />
            <url />
            <organization />
            <organizationUrl />
            <roles />
            <timezone />
            <properties />
        contributor>
    contributors>
    
    <licenses>
        
        <license>
            
            <name>Apache 2name>
            
            <url>http://www.baidu.com/banseon/LICENSE-2.0.txturl>
            
            <distribution>repodistribution>
            
            <comments>A business-friendly OSS licensecomments>
        license>
    licenses>
    
    <scm>
        
        <connection>
            scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk)
        connection>
        
        <developerConnection>
            scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk
        developerConnection>
        
        <tag />
        
        <url>http://svn.baidu.com/banseonurl>
    scm>
    
    <organization>
        
        <name>demoname>
        
        <url>http://www.baidu.com/banseonurl>
    organization>
    
    <build>
        
        <sourceDirectory />
        
        <scriptSourceDirectory />
        
        <testSourceDirectory />
        
        <outputDirectory />
        
        <testOutputDirectory />
        
        <extensions>
            
            <extension>
                
                <groupId />
                
                <artifactId />
                
                <version />
            extension>
        extensions>
        
        <defaultGoal />
        
        <resources>
            
            <resource>
                
                <targetPath />
                
                <filtering />
                
                <directory />
                
                <includes />
                
                <excludes />
            resource>
        resources>
        
        <testResources>
            
            <testResource>
                <targetPath />
                <filtering />
                <directory />
                <includes />
                <excludes />
            testResource>
        testResources>
        
        <directory />
        
        <finalName />
        
        <filters />
        
        <pluginManagement>
            
            <plugins>
                
                <plugin>
                    
                    <groupId />
                    
                    <artifactId />
                    
                    <version />
                    
                    <extensions />
                    
                    <executions>
                        
                        <execution>
                            
                            <id />
                            
                            <phase />
                            
                            <goals />
                            
                            <inherited />
                            
                            <configuration />
                        execution>
                    executions>
                    
                    <dependencies>
                        
                        <dependency>
                            ......
                        dependency>
                    dependencies>
                    
                    <inherited />
                    
                    <configuration />
                plugin>
            plugins>
        pluginManagement>
        
        <plugins>
            
            <plugin>
                <groupId />
                <artifactId />
                <version />
                <extensions />
                <executions>
                    <execution>
                        <id />
                        <phase />
                        <goals />
                        <inherited />
                        <configuration />
                    execution>
                executions>
                <dependencies>
                    
                    <dependency>
                        ......
                    dependency>
                dependencies>
                <goals />
                <inherited />
                <configuration />
            plugin>
        plugins>
    build>
    
    <profiles>
        
        <profile>
            
            <id />
            
            <activation>
                
                <activeByDefault />
                
                <jdk />
                
                <os>
                    
                    <name>Windows XPname>
                    
                    <family>Windowsfamily>
                    
                    <arch>x86arch>
                    
                    <version>5.1.2600version>
                os>
                
                <property>
                    
                    <name>mavenVersionname>
                    
                    <value>2.0.3value>
                property>
                
                <file>
                    
                    <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/
                    exists>
                    
                    <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/
                    missing>
                file>
            activation>
            
            <build>
                <defaultGoal />
                <resources>
                    <resource>
                        <targetPath />
                        <filtering />
                        <directory />
                        <includes />
                        <excludes />
                    resource>
                resources>
                <testResources>
                    <testResource>
                        <targetPath />
                        <filtering />
                        <directory />
                        <includes />
                        <excludes />
                    testResource>
                testResources>
                <directory />
                <finalName />
                <filters />
                <pluginManagement>
                    <plugins>
                        
                        <plugin>
                            <groupId />
                            <artifactId />
                            <version />
                            <extensions />
                            <executions>
                                <execution>
                                    <id />
                                    <phase />
                                    <goals />
                                    <inherited />
                                    <configuration />
                                execution>
                            executions>
                            <dependencies>
                                
                                <dependency>
                                    ......
                                dependency>
                            dependencies>
                            <goals />
                            <inherited />
                            <configuration />
                        plugin>
                    plugins>
                pluginManagement>
                <plugins>
                    
                    <plugin>
                        <groupId />
                        <artifactId />
                        <version />
                        <extensions />
                        <executions>
                            <execution>
                                <id />
                                <phase />
                                <goals />
                                <inherited />
                                <configuration />
                            execution>
                        executions>
                        <dependencies>
                            
                            <dependency>
                                ......
                            dependency>
                        dependencies>
                        <goals />
                        <inherited />
                        <configuration />
                    plugin>
                plugins>
            build>
            
            <modules />
            
            <repositories>
                
                <repository>
                    <releases>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    releases>
                    <snapshots>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    snapshots>
                    <id />
                    <name />
                    <url />
                    <layout />
                repository>
            repositories>
            
            <pluginRepositories>
                
                <pluginRepository>
                    <releases>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    releases>
                    <snapshots>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    snapshots>
                    <id />
                    <name />
                    <url />
                    <layout />
                pluginRepository>
            pluginRepositories>
            
            <dependencies>
                
                <dependency>
                    ......
                dependency>
            dependencies>
            
            <reports />
            
            <reporting>
                ......
            reporting>
            
            <dependencyManagement>
                <dependencies>
                    
                    <dependency>
                        ......
                    dependency>
                dependencies>
            dependencyManagement>
            
            <distributionManagement>
                ......
            distributionManagement>
            
            <properties />
        profile>
    profiles>
    
    <modules />
    
    <repositories>
        
        <repository>
            
            <releases>
                
                <enabled />
                
                <updatePolicy />
                
                <checksumPolicy />
            releases>
            
            <snapshots>
                <enabled />
                <updatePolicy />
                <checksumPolicy />
            snapshots>
            
            <id>banseon-repository-proxyid>
            
            <name>banseon-repository-proxyname>
            
            <url>http://192.168.1.169:9999/repository/url>
            
            <layout>defaultlayout>
        repository>
    repositories>
    
    <pluginRepositories>
        
        <pluginRepository>
            ......
        pluginRepository>
    pluginRepositories>
 
 
    
    <dependencies>
        <dependency>
            
            <groupId>org.apache.mavengroupId>
            
            <artifactId>maven-artifactartifactId>
            
            <version>3.8.1version>
            
            <type>jartype>
            
            <classifier>classifier>
            
            <scope>testscope>
            
            <systemPath>systemPath>
            
            <exclusions>
                <exclusion>
                    <artifactId>spring-coreartifactId>
                    <groupId>org.springframeworkgroupId>
                exclusion>
            exclusions>
            
            <optional>trueoptional>
        dependency>
    dependencies>
    
    <reports>reports>
    
    <reporting>
        
        <excludeDefaults />
        
        <outputDirectory />
        
        <plugins>
            
            <plugin>
                
                <groupId />
                
                <artifactId />
                
                <version />
                
                <inherited />
                
                <configuration />
                
                <reportSets>
                    
                    <reportSet>
                        
                        <id />
                        
                        <configuration />
                        
                        <inherited />
                        
                        <reports />
                    reportSet>
                reportSets>
            plugin>
        plugins>
    reporting>
    
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                ......
            dependency>
        dependencies>
    dependencyManagement>
    
    <distributionManagement>
        
        <repository>
            
            <uniqueVersion />
            <id>banseon-maven2id>
            <name>banseon maven2name>
            <url>file://${basedir}/target/deployurl>
            <layout />
        repository>
        
        <snapshotRepository>
            <uniqueVersion />
            <id>banseon-maven2id>
            <name>Banseon-maven2 Snapshot Repositoryname>
            <url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshoturl>
            <layout />
        snapshotRepository>
        
        <site>
            
            <id>banseon-siteid>
            
            <name>business api websitename>
            
            <url>
                scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web
            url>
        site>
        
        <downloadUrl />
        
        <relocation>
            
            <groupId />
            
            <artifactId />
            
            <version />
            
            <message />
        relocation>
        
        <status />
    distributionManagement>
    
    <properties />
project>

你可能感兴趣的:(插件,持续集成,maven,pom,依赖管理,构建工具)