Maven实战分析

1.文章介绍

本文是在没有任何Maven使用经验的基础上,通过查阅资料,实际项目操作记录下学习日志,并作为今后查阅的依据。

2.干货

1.什么是Maven?

本人在接触JavaCpp前,没有关注过Maven的任何相关知识,因为编译需要才开始学习Maven的使用,本人自己理解Maven和gradle一样都是定义配置,依赖,编译等流程的管理工具。官方的定义为:Apache Maven是一个软件项目管理和综合工具。基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报告和文件。

2.Maven配置

默认情况下,Maven从Maven中央仓库下载所有依赖关系。但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到。

1. Java.net资源库

添加Java.net远程仓库的详细信息在“pom.xml”文件。



    
      java.net
      https://maven.java.net/content/repositories/public/
    
 

2.JBoss Maven存储库

  1. 添加JBoss远程仓库的详细信息在 “pom.xml” 文件中。

    
      
    JBoss repository
    http://repository.jboss.org/nexus/content/groups/public/
      
    

3.pom.xml实战

要学会使用Maven,最起码的目标应该是会分析pom.xml文件的,其次是清楚的了解编译,打包,安装等Maven的生命周期。
下面对javacpp-presets项目做实战分析:
首先是javacpp-presets目录下的pom.xml:



  4.0.0
  org.bytedeco
  javacpp-presets
  1.3
  pom

  JavaCPP Presets
  The missing bridge between Java and native C++ libraries
  http://bytedeco.org/javacpp-presets/

  
    
      Apache License, Version 2.0
      http://www.apache.org/licenses/LICENSE-2.0
      repo
    
    
      GNU General Public License (GPL) version 2, or any later version
      http://www.gnu.org/licenses/
      repo
    
    
      GPLv2 with Classpath exception
      http://www.gnu.org/software/classpath/license.html
      repo
    
  

  
    
      Samuel Audet
      [email protected]
    
  

  
    opencv
    ffmpeg
    flycapture
    libdc1394
    libfreenect
    librealsense
    videoinput
    artoolkitplus
    chilitags
    flandmark
    hdf5
    openblas
    fftw
    gsl
    llvm
    leptonica
    tesseract
    caffe
    cuda
    mxnet
    tensorflow
  

  
    3.0
  

  
    https://github.com/bytedeco/javacpp-presets
    scm:git:git://github.com/bytedeco/javacpp-presets.git
    scm:git:ssh://[email protected]/bytedeco/javacpp-presets.git
  

  
    
      sonatype-nexus-snapshots
      Sonatype Nexus Snapshots
      https://oss.sonatype.org/content/repositories/snapshots/
    
    
      sonatype-nexus-staging
      Sonatype Nexus Staging
      https://oss.sonatype.org/service/local/staging/deploy/maven2/
    
  

  
    UTF-8
    false 
    false     
    false 
    ${project.artifactId}
    android-arm
    android-x86
    linux-x86
    linux-x86_64
    linux-armhf
    linux-ppc64le
    macosx-x86_64
    windows-x86
    windows-x86_64
  

  
    
      
        org.bytedeco
        javacpp
        ${project.parent.version}
      
    
  

  
    ${project.artifactId}
    
    
      
        maven-resources-plugin
        2.7
        
          
            javacpp.parser
            generate-sources
            
              resources
            
          
        
      
      
        maven-compiler-plugin
        3.3
        
          1.7
          1.7
        
        
          
            default-compile
            
              
                org/bytedeco/javacpp/*.java
              
            
          
          
            javacpp.parser
            generate-sources
            
              compile
            
            
              ${javacpp.parser.skip}
              
                org/bytedeco/javacpp/presets/*.java
              
            
          
        
      
      
        org.bytedeco
        javacpp
        ${project.parent.version}
        
          ${javacpp.platform}
          
            
              platform.root
              ${javacpp.platform.root}
            
            
              platform.compiler
              ${javacpp.platform.compiler}
            
          
          ${project.build.outputDirectory}
          ${basedir}/cppbuild/${javacpp.platform}/include/
          ${basedir}/cppbuild/${javacpp.platform}/lib/
          ${basedir}/cppbuild/${javacpp.platform}/bin/
        
        
          
            javacpp.parser
            generate-sources
            
              build
            
            
              ${javacpp.parser.skip}
              ${project.build.sourceDirectory}
              org.bytedeco.javacpp.presets.*
            
          
          
            javacpp.compiler
            process-classes
            
              build
            
            
              ${javacpp.compiler.skip}
              org.bytedeco.javacpp.*
              true
            
          
        
      
      
        maven-jar-plugin
        3.0.2
        
          
            default-jar
            package
            
              jar
            
            
              



                
                  
                  . javacpp.jar
                  org/bytedeco/javacpp/presets/
                  ${project.name}
                  Bytedeco
                  ${project.version}
                  ${project.name}
                  Bytedeco
                  ${project.version}
                
              
              
                org/bytedeco/javacpp/*
                org/bytedeco/javacpp/helper/*
                org/bytedeco/javacpp/presets/*
              
              
                org/bytedeco/javacpp/*.h
                org/bytedeco/javacpp/linux-*/
                org/bytedeco/javacpp/macosx-*/
                org/bytedeco/javacpp/windows-*/
                org/bytedeco/javacpp/${javacpp.platform}/
              
            
          
          
            ${javacpp.platform}
            package
            
              jar
            
            
              ${javacpp.platform}
              true
              
                ${javacpp.platform.library.path}/
                org/bytedeco/javacpp/${javacpp.platform}/
              
              
                **/*.exp
                **/*.lib
                **/*.obj
              
            
          
        
      
      
        maven-install-plugin
        2.5.2
        
          true
        
      
      
        maven-source-plugin
        3.0.1
        
          
            attach-source
            
              jar-no-fork
            
          
        
      
      
        maven-javadoc-plugin
        2.10.4
        
          
            attach-javadocs
            
              jar
            
            
              
                http://bytedeco.org/javacpp/apidocs/
              
            
          
        
      
      
        org.codehaus.mojo
        exec-maven-plugin
        1.5.0
        
          
            javacpp.cppbuild.install
            generate-sources
            
              exec
                        
            
              
                ${project.basedir}/../cppbuild.sh
                install
                ${javacpp.moduleId}
                -platform
                ${javacpp.platform}
              
            
          
          
            javacpp.cppbuild.clean
            clean
            
              exec
                        
            
              
                ${project.basedir}/../cppbuild.sh
                clean
                ${javacpp.moduleId}
              
            
                  
        
        
          ${javacpp.cppbuild.skip}
          ${project.basedir}/..
          bash         
        
      
    
    

    
      
        maven-deploy-plugin
        2.8.2
        
          true
        
      
      
        org.sonatype.plugins
        nexus-staging-maven-plugin
        1.6.6
        
          
            default-deploy
            deploy
            
              deploy
            
          
        
        true
        
          sonatype-nexus-staging
          https://oss.sonatype.org/
          true
        
      
    
  

  
    
      doclint-java8-disable
      
        [1.8,)
      
      
        
          
            maven-javadoc-plugin
            
              -Xdoclint:none
            
          
        
      
    

    
      assembly
      
        platform
      
    

    
      sign-artifacts
      
        
          performRelease
          true
        
      
      
        
          
            maven-gpg-plugin
            1.6
            
              
                sign-artifacts
                verify
                
                  sign
                
              
            
            
              false
            
          
        
      
    

    
      javacpp-platform-default
      
        
          !javacpp.platform
        
      
      
        ${os.name}-${os.arch}
      
    

    
      javacpp-platform-custom
      
        
          javacpp.platform
        
      
      
        ${javacpp.platform}
        ${javacpp.platform}
        ${javacpp.platform}
        ${javacpp.platform}
        ${javacpp.platform}
        ${javacpp.platform}
        ${javacpp.platform}
        ${javacpp.platform}
        ${javacpp.platform}
      
    

    
      javacpp-platform-host
      
        
          javacpp.platform.host
        
      
      
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
        ${os.name}-${os.arch}
      
    

    
      javacpp-platform-none
      
        
          javacpp.platform.none
        
      
      
        
        
        
        
        
        
        
        
        
        
      
    

    
    
      android-arm-default
      
        
          javacpp.platform
          android-arm
        
      
      
        android-arm
        ${user.home}/Android/android-ndk/
        toolchains/arm-linux-androideabi-4.9/prebuilt/${os.name}-${os.arch}/bin/arm-linux-androideabi-g++
      
    
    
      android-x86-default
      
        
          javacpp.platform
          android-x86
        
      
      
        android-x86
        ${user.home}/Android/android-ndk/
        toolchains/x86-4.9/prebuilt/${os.name}-${os.arch}/bin/i686-linux-android-g++
      
    

    
    
      linux
      
        linux
      
      
        linux
      
    
    
      macosx
      
        mac os x
      
      
        macosx
      
    
    
      windows
      
        windows
      
      
        windows
      
    
    
      i386
      
        i386
      
      
        x86
      
    
    
      i486
      
        i486
      
      
        x86
      
    
    
      i586
      
        i586
      
      
        x86
      
    
    
      i686
      
        i686
      
      
        x86
      
    
    
      amd64
      
        amd64
      
      
        x86_64
      
    
    
      x86-64
      
        x86-64
      
      
        x86_64
      
    
  


pom.xml分析:
a>基本配置

  
  4.0.0 
  
  org.bytedeco
  
  javacpp-presets
  
  1.3
  
  pom

licenses,developers分别是证书和开发者的信息。

  
    
      Apache License, Version 2.0
      http://www.apache.org/licenses/LICENSE-2.0
      repo
    
    
      GNU General Public License (GPL) version 2, or any later version
      http://www.gnu.org/licenses/
      repo
    
    
      GPLv2 with Classpath exception
      http://www.gnu.org/software/classpath/license.html
      repo
    
  

  
    
      Samuel Audet
      [email protected]
    
  

b>modules
有些maven项目会做成多模块的,这个标签用于指定当前项目所包含的所有模块。之后对这个项目进行的maven操作,会让所有子模块也进行相同操作。


    opencv
    ffmpeg
    flycapture
    libdc1394
    ...

c>prerequisites
项目构建的前提

  
    3.0
  

d>scm
允许你配置你的代码库,供Maven web站点和其它插件使用

  
    https://github.com/bytedeco/javacpp-presets
    scm:git:git://github.com/bytedeco/javacpp-presets.git
    scm:git:ssh://[email protected]/bytedeco/javacpp-presets.git
  

e>properties
用于定义pom常量,定义的常量可以在pom文件的任意地方通过${key}来引用

 
    UTF-8
    false 
    false     
    false 
    ${project.artifactId}
    android-arm
    android-x86
    linux-x86
    ...

比如可以${javacpp.platform.android-arm}引用android-arm

f>dependencyManagement
在父模块中定义后,子模块不会直接使用对应依赖,但是在使用相同依赖的时候可以不加版本号:

  
    
      
        org.bytedeco
        javacpp
        ${project.parent.version}
      
    
  

g>build

  
        
    ${project.artifactId}
        
    
    
    
        
      
        maven-resources-plugin
        2.7
             
        
           
          
            
            javacpp.parser
              
            generate-sources
              
            
              resources
            
         
        
      
  ...

h>profiles
profile可以让我们定义一系列的配置信息(插件等),然后指定其激活条件

 
    
      doclint-java8-disable
      
        [1.8,)
      
      
        
          
  ...

例如,可以选择其中的某一个模块某一个目标版本编译:
模块根据module的定义选取:
--projects xxx

  
    opencv
    ffmpeg

目标版本根据profiles的定义来选取:
-Pxxxx

 
      android-arm-default
      
        
          javacpp.platform
          android-arm
        
      
      
        android-arm
        ${user.home}/Android/android-ndk/
        toolchains/arm-linux-androideabi-4.9/prebuilt/${os.name}-${os.arch}/bin/arm-linux-androideabi-g++
      
    

建议编译时带上-X打开编译开关,可以很清楚的看到所有编译配置的属性值:
mvn install -Pandroid-arm-default --projects ffmpeg -X

root@yang:/home/yang/share/javacpp-presets1.3# mvn install -Pandroid-arm-default --projects ffmpeg -X
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java/jdk1.7.0_45/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-21-generic", arch: "amd64", family: "unix"
...
[INFO] Inspecting build with total of 1 modules...
[INFO] Not installing Nexus Staging features:
[INFO]  * Preexisting staging related goal bindings found in 1 modules.
[DEBUG] === REACTOR BUILD PLAN ================================================
[DEBUG] Project: org.bytedeco.javacpp-presets:ffmpeg:jar:3.2.1-1.3
[DEBUG] Tasks:   [install]
[DEBUG] Style:   Regular
[DEBUG] =======================================================================
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building JavaCPP Presets for FFmpeg 3.2.1-1.3
...
[DEBUG] === PROJECT BUILD PLAN ================================================
[DEBUG] Project:       org.bytedeco.javacpp-presets:ffmpeg:3.2.1-1.3
[DEBUG] Dependencies (collect): []
[DEBUG] Dependencies (resolve): [compile, runtime, test]
[DEBUG] Repositories (dependencies): [central (https://repo.maven.apache.org/maven2, default, releases)]
[DEBUG] Repositories (plugins)     : [central (https://repo.maven.apache.org/maven2, default, releases)]
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (javacpp.cppbuild.install)
[DEBUG] Style:         Regular
[DEBUG] Configuration: 

  
    /home/yang/share/javacpp-presets1.3/ffmpeg/../cppbuild.sh
    install
    ffmpeg
    -platform
    android-arm
  
  ${exec.async}
  ${exec.asyncDestroyOnShutdown}
  
  ${exec.classpathScope}
  ${exec.args}
  bash
  ${exec.longClasspath}
  ${exec.outputFile}
  
  
  false
  ${sourceRoot}
  ${testSourceRoot}
  ${exec.toolchain}
  /home/yang/share/javacpp-presets1.3/ffmpeg/..

...

然后是javacpp-presets/ffmpeg目录下的pom.xml:



  4.0.0

  
    org.bytedeco
    javacpp-presets
    1.3
  

  org.bytedeco.javacpp-presets
  ffmpeg
  3.2.1-${project.parent.version}
  JavaCPP Presets for FFmpeg

  
    
      org.bytedeco
      javacpp
    
  

  
    
      
        org.codehaus.mojo
        exec-maven-plugin
      
      
        maven-resources-plugin
      
      
        maven-compiler-plugin
      
      
        org.bytedeco
        javacpp
      
      
        maven-jar-plugin
      
      
        maven-dependency-plugin
      
      
        maven-source-plugin
      
      
        maven-javadoc-plugin
      
    
  

  
    
      mingw
      
        windows
      
      
        
          
            org.bytedeco
            javacpp
            
              ${javacpp.platform}-mingw
              
                -static-libgcc
                -static-libstdc++
                -Wl,-Bstatic
                -lstdc++
                -lgcc
                -lgcc_eh
                -lpthread
                -Wl,-Bdynamic
              
            
          
        
      
    
  


和根节点pom.xml中不一样的就是节点:

key describe
groupId 父项目的构件标识符
artifactId 父项目的唯一标识符
relativePath Maven首先在当前项目的找父项目的pom,然后在文件系统的这个位置(relativePath),然后在本地仓库,再在远程仓库找。
version 父项目的版本

分析以上两个pom.xml,可以基本了解javacpp ffmpeg编译配置。
补一张javacpp ffmpeg编译图:


Maven实战分析_第1张图片
javacpp ffmpeg编译结果

对于JavaCpp的FFMPEG编译不属于本文范畴,我会另外写一篇文章专门介绍,有兴趣的同学可以关注。

3.结束语

本人是带着很强的目的性来学习Maven的使用的,最基本的目标是根据配置好的pom.xml编译出JavaCpp的FFMPEG库(so,jar),本文也是基于这个目标记录的,或许还达不到很多同学的学习目的也解决不了实际问题,仅以此文作为自己查阅的资料吧,如有好的学习资料的实践经验,也请告诉我,一起进步,非常感谢!

你可能感兴趣的:(Maven实战分析)