springboot打包spring-boot-maven-plugin和maven-shade-plugin获取路径区别

1.spring-boot-maven-plugin打包插件

<build>
        <finalName>user-springboot</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2.maven-shade-plugin打包插件

<build>
   <finalName>user-shade</finalName>
 
    <plugins>
 
 
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.3.2</version>
           <configuration>
               <source>${java.version}</source>
               <target>${java.version}</target>
           </configuration>
       </plugin>
 
 
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-shade-plugin</artifactId>
           <dependencies>
               <dependency>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
                   <version>1.2.7.RELEASE</version>
               </dependency>
           </dependencies>
           <configuration>
               <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
               <createDependencyReducedPom>true</createDependencyReducedPom>
               <filters>
                   <filter>
                       <artifact>*:*</artifact>
                       <excludes>
                           <exclude>META-INF/*.SF</exclude>
                           <exclude>META-INF/*.DSA</exclude>
                           <exclude>META-INF/*.RSA</exclude>
                       </excludes>
                   </filter>
               </filters>
           </configuration>
           <executions>
               <execution>
                   <phase>package</phase>
                   <goals>
                       <goal>shade</goal>
                   </goals>
                   <configuration>
                       <transformers>
                           <transformer
                               implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                               <resource>META-INF/spring.handlers</resource>
                           </transformer>
                           <transformer
                               implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                               <resource>META-INF/spring.factories</resource>
                           </transformer>
                           <transformer
                               implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                               <resource>META-INF/spring.schemas</resource>
                           </transformer>
                           <transformer
                               implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                           <transformer
                               implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                               <manifestEntries>
                             <Main-Class>com.example.demo.Application</Main-Class>
                            </manifestEntries>
                           </transformer>
 
                       </transformers>
                   </configuration>
               </execution>
           </executions>
       </plugin>
 
    </plugins>
 
       <defaultGoal>compile</defaultGoal>
   </build>

3.获取路径代码

package com.example.demo.common.utils;
 
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class EnvironmentManager {
    private static final Logger logger = LoggerFactory.getLogger(EnvironmentManager.class);
     
     
    public static String content ="";
 
    public EnvironmentManager(){
     
    }
    static {
 
        //获取服务根目录
        try 
        { 
            String path = EnvironmentManager.class.getProtectionDomain().getCodeSource().getLocation().getFile();
            path = java.net.URLDecoder.decode(path, "UTF-8"); // 转换处理中文及空格 
            System.out.println("path1:"+path);
            File f = new File(path);
            String parent_path = f.getParent();
            path = java.net.URLDecoder.decode(parent_path, "UTF-8");
            System.out.println("path12:"+path);
            File f1 = new File(path);
             
            content = f1.getParent().substring(5);
            System.out.println("path3:"+content);
        } catch (java.io.UnsupportedEncodingException e) { 
            logger.error(e.getMessage());
        }
        if(!content.endsWith("/")){
            content = content+"/";  
        }
    }
}

4.路径结果打印

4.1shade打包结果

path1:/root/project/user/lib/user-shade.jar
path12:/root/project/user/lib
path3:/root/project/user

4.2spring-boot-maven-plugin打包结果

path1:file:/root/project/user/lib/user-springboot.jar!/
path12:file:/root/project/user/lib
path3:file:/root/project/user
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值