当前位置: 首页 > 工具软件 > LibreSource > 使用案例 >

springboot使用maven打包分离lib和resource

国跃
2023-12-01

springboot使用maven打包分离lib和resource

项目发版,为了应对更新多变的依赖jar包,实现增量或替换依赖jar包,越来越多的企业实现源代码和依赖jar包和依赖配置分离,更好的应对复杂多变的现场和生产环境,使用maven打包配置如下:

<build>
<plugins>
  <!-- 指定启动类,将依赖打成外部jar包 -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
        <addMavenDescriptor>false</addMavenDescriptor>
        <manifest>
          <!-- 是否要把第三方jar加入到类构建路径 -->
          <addClasspath>true</addClasspath>
          <!-- 外部依赖jar包的最终位置 -->
          <classpathPrefix>lib/</classpathPrefix>
          <!-- 项目启动类 -->
          <mainClass>com.mozi.mq_monitor.MqMonitorApplication</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <!--拷贝依赖到jar外面的lib目录-->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>copy-lib</id>
        <phase>package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>target/lib</outputDirectory>
          <excludeTransitive>false</excludeTransitive>
          <stripVersion>false</stripVersion>
          <includeScope>compile</includeScope>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <!--指定配置文件,将resources打成外部resource-->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <!-- 指定配置文件目录,这样jar运行时会去找到同目录下的resources文件夹下查找 -->
        <manifestEntries>
          <Class-Path>resources/</Class-Path>
        </manifestEntries>
      </archive>
      <!-- 打包时忽略的文件(也就是不打进jar包里的文件) -->
      <excludes>
        <exclude>*.yml</exclude>
        <exclude>*.xml</exclude>
        <exclude>mqConfig/*.xml</exclude>
      </excludes>
    </configuration>
  </plugin>
  <!-- 拷贝资源文件 外面的resource目录-->
  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <!-- 资源文件输出目录 -->
          <outputDirectory>${project.build.directory}/resources</outputDirectory>
          <resources>
            <resource>
              <directory>src/main/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>
 类似资料: