惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - 湖东

土鸡钵 工作中常见的一些英语(三) 工作中常见的一些英语(二) IDEA Community环境搭建笔记 查死锁 Jemeter使用 记录 ASP.NET并发处理 如何管理一个散漫的团队 平板开发常用框架了解 关于团队管理的一些好贴 eclipse + maven +tomcat 开发环境搭建 正则表达式30分钟入门教程(转载) java算法计算一元一次方程 前端进阶知识汇总 速度提高几百倍,记一次数据结构在实际工作中的运用(转) SQL Server应用模式之OLTP系统性能分析(转载网上的文章并结合自已的实例 ,部分脚本有做调整 ) SQL Server I/O 问题的诊断分析(转载) 一次性能优化实战经历 Sqlserver:动态性能视图:sys.dm_os_wait_stats
maven项目对象配置文件 pom.xml 解析(l转载)
湖东 · 2020-12-25 · via 博客园 - 湖东

 pom.xml是maven项目比较核心的一个文件,所以这部分的内容可以算是重头了。我还是按照描述一个对象的方式来描述这个文件吧。

 ①  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">
    <!-- 指定了当前POM的版本 -->
    <modelVersion>4.0.0</modelVersion>
</project>

  这个是pom.xml的壳子,我姑且把它叫做pom对象吧

  ② 坐标属性

  如果我们认为maven仓库是一个空间,那么坐标属性就是项目在这个空间内的 x、y、z 坐标(刚好决定坐标的标签有三个,这么比喻很合适啊:) )。

<groupId>com.sogou.hi</groupId><!-- 反写的公司网址+项目名 -->
<artifactId>hi</artifactId><!-- 一般是项目名+模块名  如 mcloud.db -->
<version>0.0.1-SNAPSHOT</version><!-- 大版本号.分支版本号.小版本号  snapshot 快照  alpha内测  beta公测  release稳定版  GA正式发布版本 -->

  任何时候要找到一个项目,只需要有这三个属性就可以了

 ③ 项目依赖列表属性

  概述中说过,这个属性是一个数组,那么在xml文件中的表现形式就是下面这样的:

复制代码

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
    </dependency>
     <dependency>
      <groupId>com.sogou.ml</groupId>
      <artifactId>ml-b</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>

复制代码

 这个依赖列表属性表明,该项目依赖了两个项目 junit和ml-b 。项目依赖还有一些其他的属性,真正用的时候是下面这样的:  

复制代码

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope><!-- scope 属性决定该依赖项目在什么阶段,test表示该项目只在测试代码中依赖 具体见  http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope -->
    </dependency>
     <dependency>
      <groupId>com.sogou.ml</groupId>
      <artifactId>ml-b</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <exclusions> <!-- 排除传递关系的依赖。 例如 ml-c 依赖 ml-b,ml-b依赖 ml-a ,那么我们会发现maven让ml-c同时依赖了a和b,通过这个属性可以排除c对a的依赖 -->
          <exclusion>
              <groupId>com.sogou.ml</groupId>
              <artifactId>ml-a</artifactId>
          </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

复制代码

④ 构建过程中使用插件的属性

 这个配置会让我们的项目在运行 mvn package打包的同时将源码也打包,原本target目录下只会出现 xxx.jar ,现在还会出现一个 xxx-source.jar

复制代码

    <!-- 为项目构建行为提供相应支持 -->
    <build>
        <!-- 插件列表 -->
        <plugins>
            <!-- 打包源码插件 -->
            <plugin>
                <!-- 插件项目坐标 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <!-- 在什么阶段执行 -->
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

复制代码

下面这个配置可以让项目打包完成后开始运行web容器,若是web项目就可以在浏览器中访问站点了。

复制代码

<plugins>
        <plugin>
               <!-- <groupId>org.mortbay.jetty</groupId>
               <artifactId>jetty-maven-plugin</artifactId>
               <version>8.1.15.v20140411</version>-->
               <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.2</version>
               <executions>
                   <!-- 打包成功后即开始运行web容器 -->
                   <execution>
                       <phase>package</phase>
                       <goals>
                           <goal>
                               run
                           </goal>
                       </goals>
                   </execution>
               </executions>
        </plugin>
    </plugins>

复制代码

 ⑤ 项目聚合属性

  假如我们有好几个项目,需要一起编译打包,挨个打包很麻烦,这个时候我们就可以用聚合属性。

复制代码

<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>com.sogou.ml</groupId>
  <artifactId>ml-all</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>ml-all</name>
  <url>http://maven.apache.org</url>
  <modules>
      <module>
          ../ml-a
      </module>
      <module>
          ../ml-b
      </module>
      <module>
          ../ml-c
      </module>
  </modules>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

复制代码

  新建一个pom文件,将packaging打包方式修改为pom。

  添加modules属性,这个属性也是一个数组,里边有一些项目文件夹的路径,编译打包这个pom项目的时候,会将modules里边的项目都编译打包。

⑥ 父子项目属性

  新建一个新的maven项目,pom修改如下

复制代码

<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>com.sogou.ml</groupId>
  <artifactId>ml-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>ml-parent</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>3.8.1</junit.version>
  </properties>
<dependencyManagement>
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
 
</project>

复制代码

  打包方式同样的修改为pom。添加<dependencyManagement>节点,这个节点下是项目依赖属性。

  在此节点下的依赖项目并不会在本项目中生效,但是可以在子项目的依赖中指定依赖,子项目配置如下:

复制代码

  <parent>
      <groupId>com.sogou.ml</groupId>
    <artifactId>ml-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
  </dependencies>

复制代码

  这样就可以让子项目依赖在父项目中配置的项目了