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

Java中Maven项目导出jar包配置的示例代码

程序员文章站 2024-03-01 11:34:16
具体代码如下所示;

具体代码如下所示;

<!-- 第一种打包方式 (maven-jar-plugin), 将依赖包和配置文件放到jar包外 -->
<build>
  <sourcedirectory>src/main/java</sourcedirectory>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <!-- 将<directory>目录下的文件打包到<targetpath>下 -->
      <targetpath>${project.build.directory}</targetpath>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </resource>
  </resources>
  <testsourcedirectory>src/test/java</testsourcedirectory>
  <testresources>
    <testresource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </testresource>
  </testresources>
  <plugins>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-compiler-plugin</artifactid>
      <version>2.3.2</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
      </configuration>
    </plugin>
    <!-- 将项目依赖包复制到<outputdirectory>指定的目录下 -->
    <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}/lib
            </outputdirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <!-- 将项目依赖包的路径(与上一项中的<outputdirectory>对应)添加到classpath中 -->
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-jar-plugin</artifactid>
      <configuration>
        <archive>
          <manifest>
            <addclasspath>true</addclasspath>
            <classpathprefix>lib/</classpathprefix>
            <mainclass>com.ctbri.echorisk.applicationstart</mainclass>
          </manifest>
          <manifestentries>
            <class-path>./</class-path>
          </manifestentries>
        </archive>
      </configuration>
    </plugin>      
  </plugins>
</build>
<!-- 第二种打包方式 (maven-shade-plugin), 将依赖包和配置文件放到jar包内 -->
<build>    
  <directory>${project.basedir}/target</directory>
  <finalname>${project.artifactid}-${project.version}</finalname>
  <plugins>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-surefire-plugin</artifactid>
      <version>2.21.0</version>
      <configuration>
        <skiptests>true</skiptests>
      </configuration>
    </plugin>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-compiler-plugin</artifactid>
      <version>2.3.2</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
        <compilerarguments>
          <extdirs>${project.basedir}/lib/</extdirs>
        </compilerarguments>
      </configuration>
    </plugin>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-shade-plugin</artifactid>
      <version>3.1.0</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <minimizejar>true</minimizejar>
            <filters>
              <filter>
                <artifact>*:*</artifact>
              </filter>
            </filters>
            <transformers>
              <!-- 往manifest文件中写入main-class是可执行包的必要条件。manifestresourcetransformer可以轻松实现。 -->
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.manifestresourcetransformer">
                <mainclass>com.ctbri.echorisk.applicationstart</mainclass>
              </transformer>
              <!-- appendingtransformer 用来处理多个jar包中存在重名的配置文件的合并,尤其是spring -->
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.appendingtransformer">
                <resource>meta-inf/spring.handlers</resource>
              </transformer>
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.appendingtransformer">
                <resource>meta-inf/spring.schemas</resource>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
<!-- 第三种打包方式 (maven-assembly-plugin) -->
<build>
  <plugins>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-surefire-plugin</artifactid>
      <version>2.21.0</version>
      <configuration>
        <skiptests>true</skiptests>
      </configuration>
    </plugin>
    <plugin>
      <artifactid>maven-compiler-plugin</artifactid>
      <version>2.3.2</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
        <compilerarguments>
          <extdirs>${project.basedir}/lib/</extdirs>
        </compilerarguments>
      </configuration>
    </plugin>
    <plugin>
      <artifactid>maven-assembly-plugin</artifactid>
      <configuration>
        <descriptorrefs>
          <descriptorref>jar-with-dependencies</descriptorref>
        </descriptorrefs>
        <archive>
          <manifest>
            <mainclass>com.ctbri.echorisk.applicationstart</mainclass>
          </manifest>
        </archive>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

总结

以上所述是小编给大家介绍的java中maven项目导出jar包配置的示例代码,希望对大家有所帮助