(小结)Maven的Pom文件

引用官网的一句话吧

The pom.xml file is the core of a project's configuration in Maven. 
It is a single configuration file that contains the majority of information required to build a project in just the way you want. 
The POM is huge and can be daunting in its complexity, 
but it is not necessary to understand all of the intricacies just yet to use it effectively. 

pom.xml文件是Maven中项目配置的核心.
它是一个单一的配置文件, 而且包含了以您希望的方式构建项目所需的大部分信息.
POM包括很多内容, 其复杂性可能令人生畏.
但没有必要了解全部内容, 够用就好了.

基本组件

properties: 一些键值对, 用于统一管理依赖包的版本, 功能类似于常量池
dependencies: 接触最多的, 配置依赖. 可以设置scope属性, 以控制依赖的存活时间(注1).
profile: 为运行环境分别设置不同的配置(注2).
build: 用于声明项目的目录结构和管理插件等(注3).

注1

功能说明
compile 默认值, 强依赖, 打包时包含. 它和provided是最常用的两个依赖关系
provided 打包时不用包进去, 因为运行环境存在该依赖
test 只用于测试, 比如junit
runntime 不参与编译, 运行时要用. 就是写代码时不用显式导包, 但是运行时会调用
system provided类似, 不使用maven构建的依赖, 而使用本地系统的依赖项

注2

含义
dev 开发环境
beta 测试环境
release 发布环境

如果未配置profile, 默认是开发环境(dev)

注3
build区域相对复杂, 因为需要在此处配置各种插件(plugin), plugin需要写在plugins中.
plugins可以直接写在build中, 也可以写在build下的pluginManagement中.
可以将pluginManagement视为插件的仓库, 在其中声明的插件不会直接被使用. 如果子pom文件中声明了 父pom的pluginManagement中已经声明过的插件, 可以直接继承相关的配置
常见插件总结如下

插件 作用
maven-shade-plugin 将指定类单独打包, 并可以控制main函数和依赖
maven-assembly-plugin 打包所有类和依赖, 一般不使用, 现在多用shade
scala-maven-plugin 支持打包scala
build-helper-maven-plugin 额外指定类和测试类所在的目录
maven-compiler-plugin 指定jdk版本

show code

源文件目录下有两个文件, 一个是java类(JavaDemo), 另一个是scala对象(ScalaDemo). 两者都有main方法.
需求是: 借助IntelliJ IDEA工具, 使用下述maven文件, 对二者进行打包
打包后会生成4个jar包: 原始一个, maven-assembly-plugin生成一个包含所有依赖的jar包, maven-shade-plugin分别指定了java和scala两个主类, 并对应生成了两个jar包
示例如下



<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.0modelVersion>

    <groupId>com.carneygroupId>
    <artifactId>workOnSparkartifactId>
    <version>1.0-SNAPSHOTversion>
	
	
	
	<properties>
        <scala.version>2.11.8scala.version>
        
        <scope.workwithout>providedscope.workwithout>
    properties>
    
	
    <dependencies>
    	
        <dependency>
            <groupId>org.scala-langgroupId>
            <artifactId>scala-libraryartifactId>
            <version>${scala.version}version>
            
            <scope>${scope.workwithout}scope>
        dependency>
    dependencies>
    
    
  	<profiles>
        <profile>
            <id>devid>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <build>
                
                <resources>
                    <resource>
                        <directory>/src/main/resourcesdirectory>
                        <filtering>truefiltering>
                    resource>
                resources>
                
                
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-shade-pluginartifactId>
                    plugin>
                plugins>
                
            build>
        profile>
    profiles>
  

  <build>
      
      <outputDirectory>target/classesoutputDirectory>
      <testOutputDirectory>target/test-classestestOutputDirectory>

    <pluginManagement>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.pluginsgroupId>
              <artifactId>maven-shade-pluginartifactId>
              <version>3.2.1version>
              
              <executions>
					<execution>
                      
                      <id>JavaDemoShadeid>
                      <phase>packagephase>
                      <goals>
                          <goal>shadegoal>
                      goals>
                      <configuration>
                          
                          <minimizeJar>falseminimizeJar>
                          
                          <shadedArtifactAttached>trueshadedArtifactAttached>
                          <shadedClassifierName>JavaDemoShadeshadedClassifierName>
                      configuration>
                  execution>
                  
                  
                  <execution>
                      <id>ScalaDemoShadeid>
                      <phase>packagephase>
                      <goals>
                          <goal>shadegoal>
                      goals>
                      <configuration>
                          <shadedArtifactAttached>trueshadedArtifactAttached>
                          <shadedClassifierName>ScalaDemoShadeshadedClassifierName>
                          
                          <filters>
                              
                              <filter>
                                  <artifact>*:*artifact>
                                  <excludes>
                                      <exclude>META-INF/*.SFexclude>
                                      <exclude>META-INF/*.DSAexclude>
                                      <exclude>META-INF/*.RSAexclude>
                                  excludes>
                              filter>
                          filters>
                          <transformers>
                              
                              <transformer
                                      implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                  <resource>reference.confresource>
                              transformer>
                              
                              <transformer
                                      implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                  <mainClass>ScalaDemomainClass>
                              transformer>
                          transformers>
                      configuration>
                  execution>
              executions>
          plugin>
        plugins>
      pluginManagement>

      <plugins>
            
            <plugin>
                <artifactId>maven-assembly-pluginartifactId>
                <version>2.4.1version>
                <configuration>
                    <descriptorRefs>
                        
                        <descriptorRef>jar-with-dependenciesdescriptorRef>
                    descriptorRefs>
                configuration>
                <executions>
                    <execution>
                        <id>make-assemblyid>
                        <phase>packagephase>
                        <goals>
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>

            
            <plugin>
                <groupId>net.alchim31.mavengroupId>
                <artifactId>scala-maven-pluginartifactId>
                <version>4.0.2version>
                <executions>
                    <execution>
                        
                        <id>scala-compile-firstid>
                        <phase>process-resourcesphase>
                        <goals>
                            <goal>compilegoal>
                        goals>
                    execution>
                    <execution>
                        
                        <id>scala-test-compileid>
                        <phase>process-test-resourcesphase>
                        <goals>
                            <goal>testCompilegoal>
                        goals>
                    execution>
                executions>
            plugin>

            
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.6.1version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
          
          	
            
            <plugin>
                <groupId>org.codehaus.mojogroupId>
                <artifactId>build-helper-maven-pluginartifactId>
                <version>3.0.0version>
                <executions>
                    
                    <execution>
                        <id>add-sourceid>
                        <phase>generate-sourcesphase>
                        <goals>
                            <goal>add-sourcegoal>
                        goals>
                        <configuration>
                            <sources>
                                <source>src/main/scalasource>
                            sources>
                        configuration>
                    execution>
                    
                    <execution>
                        <id>add-test-sourceid>
                        <phase>generate-sourcesphase>
                        <goals>
                            <goal>add-test-sourcegoal>
                        goals>
                        <configuration>
                            <sources>
                                <source>src/test/scalasource>
                            sources>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

你可能感兴趣的:(使用说明)