当前位置:   article > 正文

maven、gradle 设置MANIFEST.MF配置_maven manifest配置

maven manifest配置

一、说明

在使用maven、gradle对java工程打包时,可能会有需求将依赖包单独输出到目录中,并且设置启动jar包的Class-Path的需求。这里记录一下。

二、gradle 设置MANIFEST.MF

在build.gradle 文件中 添加如下配置

plugins {
    id 'java'
    id 'application'
}

// 定义被引用的全部依赖包
def allJars = (configurations.runtimeClasspath)
        .findAll{it.isFile()}
        .collect { 'lib/' + it.name }
        .join(' ')
 //定义启动类
mainClassName = 'com.example.App'
// 定义拷贝依赖的task  将依赖输出至lib目录
task copyDependencies(type: Copy){
    from configurations.runtime
    into "$rootDir/lib"
}
//  gradle jar 执行 同时也执行 copyDependencies task
jar.dependsOn(copyDependencies)
// 设置MANIFEST.MF Main-Class 和Class-Path
jar {
    manifest {
        attributes(
                'Main-Class': mainClassName,
                'Class-Path': allJars + " .",
        )
    }
    // 将全部的依赖库打入jar包
//    into('lib') {
//        from configurations.runtime
//    }
}

application {
	    mainClassName
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

gradle 打包命令 gradle jar

三、maven 设置MANIFEST.MF

在pom.xml中添加如下设置

 <build>
      <plugins>
        <!-- 打包 设置 MANIFEST.MF 文件中 Class-Path: 依赖-->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.6</version>
          <configuration>
            <archive>
              <manifest>
              <!-- 添加依赖包路径, 路径会写在MANIFEST文件的Class-Path下 -->
                <addClasspath>true</addClasspath>
                <!-- 添加依赖前缀 -->
                <classpathPrefix>lib/</classpathPrefix>
                <!-- 设置启动函数 -->
                <mainClass>com.example.App</mainClass>
              </manifest>
              <manifestEntries>
              <!-- 自定义 MANIFEST.MC文件中其他属性 -->
                <Class-Path>.</Class-Path>
              </manifestEntries>
            </archive>
          </configuration>
        </plugin>
        <!--将依赖输出到 target/lib 目录下-->
        <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>
              <!-- 依赖输出target/lib目录 -->
              <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
  </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

maven 打包命令:mvn clean package
maven 安装包到本地仓库:mvn clean install

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/265505
推荐阅读
相关标签
  

闽ICP备14008679号