欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

maven打可执行jar包(配置文件在jar包外)

程序员文章站 2024-03-13 20:50:29
...

pom.xml文件内容

<build>
    <resources>
      <!--指定src/main/resources资源要过滤-->
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <plugins>
      <!-- 可执行jar插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <outputDirectory>${project.build.directory}/java</outputDirectory>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>com.ft.mq.App</mainClass>
            </manifest>
            <manifestEntries>
              <Class-Path>.</Class-Path>
            </manifestEntries>
          </archive>
          <excludes>
            <exclude>**/*.xml</exclude>
            <exclude>**/*.conf</exclude>
            <exclude>**/*.properties</exclude>
          </excludes>

        </configuration>
      </plugin>
      <!-- maven资源文件复制插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/java</outputDirectory>
              <resources>
                <resource>
                  <directory>src/main/resources</directory>
                  <includes>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.conf</exclude>
                    <exclude>**/*.properties</exclude>
                  </includes>
                  <filtering>true</filtering>
                </resource>
              </resources>
              <encoding>UTF-8</encoding>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- 依赖包插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/java/lib</outputDirectory>
              <!-- 是否不包含间接依赖 -->
              <excludeTransitive>false</excludeTransitive>
              <!-- 忽略版本 -->
              <stripVersion>false</stripVersion>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
    </plugins>
  </build>

打包后效果

maven打可执行jar包(配置文件在jar包外)