【工具学习】maven打包工具 - hippowc/hippowc.github.io GitHub Wiki

一个最简单的pom如何配置

group,artifactId,version就构成了一个最简答pom,如果没有引入其他的依赖,这个就可以用来打包了,如果用到了其他功能,就需要研究下标签。

maven执行的各种命令都是使用插件执行的,如果不配置build标签,就是使用默认配置的插件执行,如果要修改插件的一些功能就要在build标签中进行相应的修改

<!-- build根标签 -->
<build>
  
  <!-- 指定打包文件名称(可用于除去jar文件版本号) -->
  <finalName>maven-build-demo</finalName>
  
  <!-- 指定过滤资源目录 -->
  <filters>
    <filter>${basedir}/profiles/test/test.properties</filter>
  </filters>
  
  <!-- 项目资源清单(可以配置多个项目资源) -->
  <resources>
    <!-- 项目资源  -->
    <resource>
      <!-- 资源目录(编译时会将指定资源目录中的内容复制到输出目录) -->
      <directory>src/main/resources</directory>
      <!-- 包含内容(编译时仅复制指定包含的内容) -->
      <includes>
        <include>*.properties</include>
        <include>*.xml</include>
        <include>*.json</include>
      </includes>
      <!-- 排除内容(编译时不复制指定排除的内容) -->
      <excludes>
        <exclude>*.txt</exclude>
      </excludes>
      <!-- 输出目录(默认为${build.outputDirectory},即target/classes) -->
      <targetPath>${build.outputDirectory}</targetPath>
      <!-- 是否开启资源过滤(需要引入maven-resources-plugin插件)
       |   true:将用过滤资源(filters标签)中的内容 替换 资源中相应的占位符(${Xxxx})内容
       |   false:不做过滤替换操作
       -->
      <filtering>true</filtering>
    </resource>
  </resources>
  
  <!-- 项目插件清单(需要用到什么插件,就添加什么插件) -->
  <plugins>
    <!-- 项目插件 -->
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <!-- 配置参数 -->
      <configuration>
        <!-- 设为可执行程序 -->
        <executable>true</executable>
      </configuration>
      <executions>
        <!-- 执行操作 -->
        <execution>
          <!-- 执行目标 -->
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <!-- 项目插件 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <!-- 执行操作清单 -->
      <executions>
        <!-- 执行操作(示例说明:将项目的父级目录下的profiles下的指定文件复制到指定输出目录) -->
        <execution>
          <!-- 标识ID -->
          <id>copy-resources</id>
          <!-- 执行阶段 -->
          <phase>validate</phase>
          <!-- 执行目标(执行的操作) -->
          <goals>
            <goal>copy-resources</goal>
          </goals>
          <!-- 相关参数 -->
          <configuration>
            <outputDirectory>${basedir}/target/classes/</outputDirectory>
            <resources>
              <resource>
                <directory>${basedir}/../profiles</directory>
                <filtering>false</filtering>
                <includes>
                  <include>**/*.xml</include>
                  <include>*.json</include>
                </includes>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
  • 问题:错误找不到符号。

在pom中引入依赖后没有起作用,还是找不到相应的类,但是换了一个依赖包的版本,就可以编译通过了。ps:另外一个问题,maven编译通过了,如何在IDE中也配置编译通过

dependency 插件

使用maven解决jar冲突是最常见的用法。命令:

mvn dependency:tree > tree.log

如果要打印详细决策过程

mvn dependency:tree -Dverbose > tree.log

其中:

  • version managed from xxx,表示本来依赖xxx,但是项目明确指定当前版本
  • omitted for duplicate 版本号相同,当前jar包引入的被忽略
  • omitted for conflict with xxxx 说明和别的jar包版本冲突了,而该行的jar包不会被引入

支持只打印感兴趣的包, includes 支持通配符的形式

-Dincludes=*guava*

maven如何依赖本地下载的jar

可以使用命令,经本地jar包安装到repository

mvn install:install-file -Dfile=taobao-sdk-java-auto_1479188381469-20200228.jar -DgroupId=com.alibaba.dingding -DartifactId=ding-sdk -Dversion=1.0.0 -Dpackaging=jar

然后maven会将jar安装到如下目录,并重命名

Installing /Users/chenwen.wcw/Documents/work/test/dingtalk-sdk-java/taobao-sdk-java-auto_1479188381469-20200228.jar to /Users/user/.m2/repository/com/alibaba/dingding/ding-sdk/1.0.0/ding-sdk-1.0.0.jar
⚠️ **GitHub.com Fallback** ⚠️