maven开发者首页推荐

Maven实战分析

2017-12-08  本文已影响41人  Young_Allen

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”文件。

<project ...>
<repositories>
    <repository>
      <id>java.net</id>
      <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
 </repositories>
</project>

2.JBoss Maven存储库

  1. 添加JBoss远程仓库的详细信息在 “pom.xml” 文件中。
<project ...>
    <repositories>
      <repository>
    <id>JBoss repository</id>
    <url>http://repository.jboss.org/nexus/content/groups/public/</url>
      </repository>
    </repositories>
</project>

3.pom.xml实战

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

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.bytedeco</groupId>
  <artifactId>javacpp-presets</artifactId>
  <version>1.3</version>
  <packaging>pom</packaging>

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

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

  <developers>
    <developer>
      <name>Samuel Audet</name>
      <email>samuel.audet@gmail.com</email>
    </developer>
  </developers>

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

  <prerequisites>
    <maven>3.0</maven>
  </prerequisites>

  <scm>
    <url>https://github.com/bytedeco/javacpp-presets</url>
    <connection>scm:git:git://github.com/bytedeco/javacpp-presets.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/bytedeco/javacpp-presets.git</developerConnection>
  </scm>

  <distributionManagement>
    <snapshotRepository>
      <id>sonatype-nexus-snapshots</id>
      <name>Sonatype Nexus Snapshots</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </snapshotRepository>
    <repository>
      <id>sonatype-nexus-staging</id>
      <name>Sonatype Nexus Staging</name>
      <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    </repository>
  </distributionManagement>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <javacpp.cppbuild.skip>false</javacpp.cppbuild.skip> <!-- To skip execution of cppbuild.sh: -Djavacpp.cppbuild.skip=true -->
    <javacpp.parser.skip>false</javacpp.parser.skip>     <!-- To skip header file parsing phase: -Djavacpp.parser.skip=true  -->
    <javacpp.compiler.skip>false</javacpp.compiler.skip> <!-- To skip native compilation phase: -Djavacpp.compiler.skip=true -->
    <javacpp.moduleId>${project.artifactId}</javacpp.moduleId>
    <javacpp.platform.android-arm>android-arm</javacpp.platform.android-arm>
    <javacpp.platform.android-x86>android-x86</javacpp.platform.android-x86>
    <javacpp.platform.linux-x86>linux-x86</javacpp.platform.linux-x86>
    <javacpp.platform.linux-x86_64>linux-x86_64</javacpp.platform.linux-x86_64>
    <javacpp.platform.linux-armhf>linux-armhf</javacpp.platform.linux-armhf>
    <javacpp.platform.linux-ppc64le>linux-ppc64le</javacpp.platform.linux-ppc64le>
    <javacpp.platform.macosx-x86_64>macosx-x86_64</javacpp.platform.macosx-x86_64>
    <javacpp.platform.windows-x86>windows-x86</javacpp.platform.windows-x86>
    <javacpp.platform.windows-x86_64>windows-x86_64</javacpp.platform.windows-x86_64>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
        <version>${project.parent.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>${project.artifactId}</finalName>
    <pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>javacpp.parser</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
        <executions>
          <execution>
            <id>default-compile</id>
            <configuration>
              <includes>
                <include>org/bytedeco/javacpp/*.java</include>
              </includes>
            </configuration>
          </execution>
          <execution>
            <id>javacpp.parser</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <skipMain>${javacpp.parser.skip}</skipMain>
              <includes>
                <include>org/bytedeco/javacpp/presets/*.java</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
        <version>${project.parent.version}</version>
        <configuration>
          <properties>${javacpp.platform}</properties>
          <propertyKeysAndValues>
            <property>
              <name>platform.root</name>
              <value>${javacpp.platform.root}</value>
            </property>
            <property>
              <name>platform.compiler</name>
              <value>${javacpp.platform.compiler}</value>
            </property>
          </propertyKeysAndValues>
          <classPath>${project.build.outputDirectory}</classPath>
          <includePath>${basedir}/cppbuild/${javacpp.platform}/include/</includePath>
          <linkPath>${basedir}/cppbuild/${javacpp.platform}/lib/</linkPath>
          <preloadPath>${basedir}/cppbuild/${javacpp.platform}/bin/</preloadPath>
        </configuration>
        <executions>
          <execution>
            <id>javacpp.parser</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>build</goal>
            </goals>
            <configuration>
              <skip>${javacpp.parser.skip}</skip>
              <outputDirectory>${project.build.sourceDirectory}</outputDirectory>
              <classOrPackageName>org.bytedeco.javacpp.presets.*</classOrPackageName>
            </configuration>
          </execution>
          <execution>
            <id>javacpp.compiler</id>
            <phase>process-classes</phase>
            <goals>
              <goal>build</goal>
            </goals>
            <configuration>
              <skip>${javacpp.compiler.skip}</skip>
              <classOrPackageName>org.bytedeco.javacpp.*</classOrPackageName>
              <copyLibs>true</copyLibs>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <archive>
<!--                <manifest>                          -->
<!--                  <addClasspath>true</addClasspath> -->
<!--                </manifest>                         -->
                <manifestEntries>
                  <!-- <addClasspath/> is broken: http://jira.codehaus.org/browse/MJAR-61 -->
                  <Class-Path>. javacpp.jar</Class-Path>
                  <Name>org/bytedeco/javacpp/presets/</Name>
                  <Implementation-Title>${project.name}</Implementation-Title>
                  <Implementation-Vendor>Bytedeco</Implementation-Vendor>
                  <Implementation-Version>${project.version}</Implementation-Version>
                  <Specification-Title>${project.name}</Specification-Title>
                  <Specification-Vendor>Bytedeco</Specification-Vendor>
                  <Specification-Version>${project.version}</Specification-Version>
                </manifestEntries>
              </archive>
              <includes>
                <include>org/bytedeco/javacpp/*</include>
                <include>org/bytedeco/javacpp/helper/*</include>
                <include>org/bytedeco/javacpp/presets/*</include>
              </includes>
              <excludes>
                <exclude>org/bytedeco/javacpp/*.h</exclude>
                <exclude>org/bytedeco/javacpp/linux-*/</exclude>
                <exclude>org/bytedeco/javacpp/macosx-*/</exclude>
                <exclude>org/bytedeco/javacpp/windows-*/</exclude>
                <exclude>org/bytedeco/javacpp/${javacpp.platform}/</exclude>
              </excludes>
            </configuration>
          </execution>
          <execution>
            <id>${javacpp.platform}</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <classifier>${javacpp.platform}</classifier>
              <skipIfEmpty>true</skipIfEmpty>
              <includes>
                <include>${javacpp.platform.library.path}/</include>
                <include>org/bytedeco/javacpp/${javacpp.platform}/</include>
              </includes>
              <excludes>
                <exclude>**/*.exp</exclude>
                <exclude>**/*.lib</exclude>
                <exclude>**/*.obj</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
        <configuration>
          <createChecksum>true</createChecksum>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
          <execution>
            <id>attach-source</id>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.10.4</version>
        <executions>
          <execution>
            <id>attach-javadocs</id>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <links>
                <link>http://bytedeco.org/javacpp/apidocs/</link>
              </links>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.5.0</version>
        <executions>
          <execution>
            <id>javacpp.cppbuild.install</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>            
            <configuration>
              <arguments>
                <argument>${project.basedir}/../cppbuild.sh</argument>
                <argument>install</argument>
                <argument>${javacpp.moduleId}</argument>
                <argument>-platform</argument>
                <argument>${javacpp.platform}</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>javacpp.cppbuild.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>exec</goal>
            </goals>            
            <configuration>
              <arguments>
                <argument>${project.basedir}/../cppbuild.sh</argument>
                <argument>clean</argument>
                <argument>${javacpp.moduleId}</argument>
              </arguments>
            </configuration>
          </execution>        
        </executions>
        <configuration>
          <skip>${javacpp.cppbuild.skip}</skip>
          <workingDirectory>${project.basedir}/..</workingDirectory>
          <executable>bash</executable>         
        </configuration>
      </plugin>
    </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.sonatype.plugins</groupId>
        <artifactId>nexus-staging-maven-plugin</artifactId>
        <version>1.6.6</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
        <extensions>true</extensions>
        <configuration>
          <serverId>sonatype-nexus-staging</serverId>
          <nexusUrl>https://oss.sonatype.org/</nexusUrl>
          <skipStagingRepositoryClose>true</skipStagingRepositoryClose>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>doclint-java8-disable</id>
      <activation>
        <jdk>[1.8,)</jdk>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-javadoc-plugin</artifactId>
            <configuration>
              <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>assembly</id>
      <modules>
        <module>platform</module>
      </modules>
    </profile>

    <profile>
      <id>sign-artifacts</id>
      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.6</version>
            <executions>
              <execution>
                <id>sign-artifacts</id>
                <phase>verify</phase>
                <goals>
                  <goal>sign</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <useAgent>false</useAgent>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>javacpp-platform-default</id>
      <activation>
        <property>
          <name>!javacpp.platform</name>
        </property>
      </activation>
      <properties>
        <javacpp.platform>${os.name}-${os.arch}</javacpp.platform>
      </properties>
    </profile>

    <profile>
      <id>javacpp-platform-custom</id>
      <activation>
        <property>
          <name>javacpp.platform</name>
        </property>
      </activation>
      <properties>
        <javacpp.platform.android-arm>${javacpp.platform}</javacpp.platform.android-arm>
        <javacpp.platform.android-x86>${javacpp.platform}</javacpp.platform.android-x86>
        <javacpp.platform.linux-x86>${javacpp.platform}</javacpp.platform.linux-x86>
        <javacpp.platform.linux-x86_64>${javacpp.platform}</javacpp.platform.linux-x86_64>
        <javacpp.platform.linux-armhf>${javacpp.platform}</javacpp.platform.linux-armhf>
        <javacpp.platform.linux-ppc64le>${javacpp.platform}</javacpp.platform.linux-ppc64le>
        <javacpp.platform.macosx-x86_64>${javacpp.platform}</javacpp.platform.macosx-x86_64>
        <javacpp.platform.windows-x86>${javacpp.platform}</javacpp.platform.windows-x86>
        <javacpp.platform.windows-x86_64>${javacpp.platform}</javacpp.platform.windows-x86_64>
      </properties>
    </profile>

    <profile>
      <id>javacpp-platform-host</id>
      <activation>
        <property>
          <name>javacpp.platform.host</name>
        </property>
      </activation>
      <properties>
        <javacpp.platform>${os.name}-${os.arch}</javacpp.platform>
        <javacpp.platform.android-arm>${os.name}-${os.arch}</javacpp.platform.android-arm>
        <javacpp.platform.android-x86>${os.name}-${os.arch}</javacpp.platform.android-x86>
        <javacpp.platform.linux-x86>${os.name}-${os.arch}</javacpp.platform.linux-x86>
        <javacpp.platform.linux-x86_64>${os.name}-${os.arch}</javacpp.platform.linux-x86_64>
        <javacpp.platform.linux-armhf>${os.name}-${os.arch}</javacpp.platform.linux-armhf>
        <javacpp.platform.linux-ppc64le>${os.name}-${os.arch}</javacpp.platform.linux-ppc64le>
        <javacpp.platform.macosx-x86_64>${os.name}-${os.arch}</javacpp.platform.macosx-x86_64>
        <javacpp.platform.windows-x86>${os.name}-${os.arch}</javacpp.platform.windows-x86>
        <javacpp.platform.windows-x86_64>${os.name}-${os.arch}</javacpp.platform.windows-x86_64>
      </properties>
    </profile>

    <profile>
      <id>javacpp-platform-none</id>
      <activation>
        <property>
          <name>javacpp.platform.none</name>
        </property>
      </activation>
      <properties>
        <javacpp.platform></javacpp.platform>
        <javacpp.platform.android-arm></javacpp.platform.android-arm>
        <javacpp.platform.android-x86></javacpp.platform.android-x86>
        <javacpp.platform.linux-x86></javacpp.platform.linux-x86>
        <javacpp.platform.linux-x86_64></javacpp.platform.linux-x86_64>
        <javacpp.platform.linux-armhf></javacpp.platform.linux-armhf>
        <javacpp.platform.linux-ppc64le></javacpp.platform.linux-ppc64le>
        <javacpp.platform.macosx-x86_64></javacpp.platform.macosx-x86_64>
        <javacpp.platform.windows-x86></javacpp.platform.windows-x86>
        <javacpp.platform.windows-x86_64></javacpp.platform.windows-x86_64>
      </properties>
    </profile>

    <!-- Provide convenient profiles to set commonly used paths for Android -->
    <profile>
      <id>android-arm-default</id>
      <activation>
        <property>
          <name>javacpp.platform</name>
          <value>android-arm</value>
        </property>
      </activation>
      <properties>
        <javacpp.platform>android-arm</javacpp.platform>
        <javacpp.platform.root>${user.home}/Android/android-ndk/</javacpp.platform.root>
        <javacpp.platform.compiler>toolchains/arm-linux-androideabi-4.9/prebuilt/${os.name}-${os.arch}/bin/arm-linux-androideabi-g++</javacpp.platform.compiler>
      </properties>
    </profile>
    <profile>
      <id>android-x86-default</id>
      <activation>
        <property>
          <name>javacpp.platform</name>
          <value>android-x86</value>
        </property>
      </activation>
      <properties>
        <javacpp.platform>android-x86</javacpp.platform>
        <javacpp.platform.root>${user.home}/Android/android-ndk/</javacpp.platform.root>
        <javacpp.platform.compiler>toolchains/x86-4.9/prebuilt/${os.name}-${os.arch}/bin/i686-linux-android-g++</javacpp.platform.compiler>
      </properties>
    </profile>

    <!-- If someone knows a better way to do this, please do let me know! -->
    <profile>
      <id>linux</id>
      <activation>
        <os><name>linux</name></os>
      </activation>
      <properties>
        <os.name>linux</os.name>
      </properties>
    </profile>
    <profile>
      <id>macosx</id>
      <activation>
        <os><name>mac os x</name></os>
      </activation>
      <properties>
        <os.name>macosx</os.name>
      </properties>
    </profile>
    <profile>
      <id>windows</id>
      <activation>
        <os><family>windows</family></os>
      </activation>
      <properties>
        <os.name>windows</os.name>
      </properties>
    </profile>
    <profile>
      <id>i386</id>
      <activation>
        <os><arch>i386</arch></os>
      </activation>
      <properties>
        <os.arch>x86</os.arch>
      </properties>
    </profile>
    <profile>
      <id>i486</id>
      <activation>
        <os><arch>i486</arch></os>
      </activation>
      <properties>
        <os.arch>x86</os.arch>
      </properties>
    </profile>
    <profile>
      <id>i586</id>
      <activation>
        <os><arch>i586</arch></os>
      </activation>
      <properties>
        <os.arch>x86</os.arch>
      </properties>
    </profile>
    <profile>
      <id>i686</id>
      <activation>
        <os><arch>i686</arch></os>
      </activation>
      <properties>
        <os.arch>x86</os.arch>
      </properties>
    </profile>
    <profile>
      <id>amd64</id>
      <activation>
        <os><arch>amd64</arch></os>
      </activation>
      <properties>
        <os.arch>x86_64</os.arch>
      </properties>
    </profile>
    <profile>
      <id>x86-64</id>
      <activation>
        <os><arch>x86-64</arch></os>
      </activation>
      <properties>
        <os.arch>x86_64</os.arch>
      </properties>
    </profile>
  </profiles>

</project>

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

  <!--pom模型版本,maven2和3只能为4.0.0-->
  <modelVersion>4.0.0</modelVersion> 
  <!--组ID,maven用于定位-->
  <groupId>org.bytedeco</groupId>
  <!--在组中的唯一ID用于定位-->
  <artifactId>javacpp-presets</artifactId>
  <!--项目版本-->
  <version>1.3</version>
  <!--项目打包方式,有以下值:pom, jar, maven-plugin, ejb, war, ear, rar, par-->
  <packaging>pom</packaging>

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

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

  <developers>
    <developer>
      <name>Samuel Audet</name>
      <email>samuel.audet@gmail.com</email>
    </developer>
  </developers>

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

<modules>
    <module>opencv</module>
    <module>ffmpeg</module>
    <module>flycapture</module>
    <module>libdc1394</module>
    ...
</modules>

c>prerequisites
项目构建的前提

  <prerequisites>
    <maven>3.0</maven>
  </prerequisites>

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

  <scm>
    <url>https://github.com/bytedeco/javacpp-presets</url>
    <connection>scm:git:git://github.com/bytedeco/javacpp-presets.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/bytedeco/javacpp-presets.git</developerConnection>
  </scm>

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

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <javacpp.cppbuild.skip>false</javacpp.cppbuild.skip> <!-- To skip execution of cppbuild.sh: -Djavacpp.cppbuild.skip=true -->
    <javacpp.parser.skip>false</javacpp.parser.skip>     <!-- To skip header file parsing phase: -Djavacpp.parser.skip=true  -->
    <javacpp.compiler.skip>false</javacpp.compiler.skip> <!-- To skip native compilation phase: -Djavacpp.compiler.skip=true -->
    <javacpp.moduleId>${project.artifactId}</javacpp.moduleId>
    <javacpp.platform.android-arm>android-arm</javacpp.platform.android-arm>
    <javacpp.platform.android-x86>android-x86</javacpp.platform.android-x86>
    <javacpp.platform.linux-x86>linux-x86</javacpp.platform.linux-x86>
    ...
</properties>

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

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

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
        <version>${project.parent.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

g>build

  <build>
    <!--产生的构件的文件名,默认值是${artifactId}-${version}。-->    
    <finalName>${project.artifactId}</finalName>
    <!--子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置-->    
    <pluginManagement>
    <!--使用的插件列表 。-->
    <plugins>
      <!--plugin元素包含描述插件所需要的信息。-->  
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。-->     
        <executions>
          <!--execution元素包含了插件执行需要的信息--> 
          <execution>
            <!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标-->
            <id>javacpp.parser</id>
            <!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段-->  
            <phase>generate-sources</phase>
            <!--配置的执行目标-->  
            <goals>
              <goal>resources</goal>
            </goals>
         </execution>
        </executions>
      </plugin>
  ...

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

 <profiles>
    <profile>
      <id>doclint-java8-disable</id>
      <activation>
        <jdk>[1.8,)</jdk>
      </activation>
      <build>
        <plugins>
          <plugin>
  ...

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

  <modules>
    <module>opencv</module>
    <module>ffmpeg</module>

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

 <profile>
      <id>android-arm-default</id>
      <activation>
        <property>
          <name>javacpp.platform</name>
          <value>android-arm</value>
        </property>
      </activation>
      <properties>
        <javacpp.platform>android-arm</javacpp.platform>
        <javacpp.platform.root>${user.home}/Android/android-ndk/</javacpp.platform.root>
        <javacpp.platform.compiler>toolchains/arm-linux-androideabi-4.9/prebuilt/${os.name}-${os.arch}/bin/arm-linux-androideabi-g++</javacpp.platform.compiler>
      </properties>
    </profile>

建议编译时带上-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: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <arguments>
    <argument>/home/yang/share/javacpp-presets1.3/ffmpeg/../cppbuild.sh</argument>
    <argument>install</argument>
    <argument>ffmpeg</argument>
    <argument>-platform</argument>
    <argument>android-arm</argument>
  </arguments>
  <async default-value="false">${exec.async}</async>
  <asyncDestroyOnShutdown default-value="true">${exec.asyncDestroyOnShutdown}</asyncDestroyOnShutdown>
  <basedir default-value="${basedir}"/>
  <classpathScope default-value="runtime">${exec.classpathScope}</classpathScope>
  <commandlineArgs>${exec.args}</commandlineArgs>
  <executable>bash</executable>
  <longClasspath default-value="false">${exec.longClasspath}</longClasspath>
  <outputFile>${exec.outputFile}</outputFile>
  <project default-value="${project}"/>
  <session default-value="${session}"/>
  <skip default-value="false">false</skip>
  <sourceRoot>${sourceRoot}</sourceRoot>
  <testSourceRoot>${testSourceRoot}</testSourceRoot>
  <toolchain default-value="jdk">${exec.toolchain}</toolchain>
  <workingDirectory>/home/yang/share/javacpp-presets1.3/ffmpeg/..</workingDirectory>
</configuration>
...

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

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacpp-presets</artifactId>
    <version>1.3</version>
  </parent>

  <groupId>org.bytedeco.javacpp-presets</groupId>
  <artifactId>ffmpeg</artifactId>
  <version>3.2.1-${project.parent.version}</version>
  <name>JavaCPP Presets for FFmpeg</name>

  <dependencies>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacpp</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-javadoc-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>mingw</id>
      <activation>
        <os><family>windows</family></os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <configuration>
              <properties>${javacpp.platform}-mingw</properties>
              <compilerOptions>
                <compilerOption>-static-libgcc</compilerOption>
                <compilerOption>-static-libstdc++</compilerOption>
                <compilerOption>-Wl,-Bstatic</compilerOption>
                <compilerOption>-lstdc++</compilerOption>
                <compilerOption>-lgcc</compilerOption>
                <compilerOption>-lgcc_eh</compilerOption>
                <compilerOption>-lpthread</compilerOption>
                <compilerOption>-Wl,-Bdynamic</compilerOption>
              </compilerOptions>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>

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

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

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


javacpp ffmpeg编译结果

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

3.结束语

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

上一篇 下一篇

猜你喜欢

热点阅读