1.背景说明,辅助阅读和参考方向
idea (2020.3版本)编写H3c华三云接口时候,用 maven 管理项目jar,用 gitlab 管理项目。
当项目构建差不多时候,并test没用问题后,准备使用 maven clean,maven package时候出现问题,问题下 一节有截图。
版本:jdk1.8.1 其他版本和项目插件 plugin 版本均有截图:
项目project中使用 maven管理,并在package时候出现问题时候,可以参考这文章,其他酌情参考。
2. 问题出现
2-1.问题出现前说明:(仔细看每个图的文字,按照顺序看)
2-2问题附图:【主要解决的问题是这个截图】
3.解决
3-1.试着解决:
首先,2中提到解决的方式之一是在springboot下解除其中一个run类,具体报错具体解决,然而处理后,重新 maven package时候还是报同样错;
其次,在pom.xml添加maven-plugin下的 execution中的repackage参数,,依然没解决 如下配置:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
最后:可以解决的是: 在compiler-plugin中添加excludes下的排除一些包和类:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <!-- 跳过测试代码的编译 --> <skip>true</skip> <!--排除的--> <excludes> <exclude>**/hwapi/**</exclude> <exclude>**/TestMain.java</exclude> </excludes> </configuration> </plugin>
3-2:附加提升package要求
附上一些打包需求:在target时候,不想要所有的包编译进去,即:调试类的程序等不想一块打包到 jar库 包里面去,这个就需要在使用 maven 打包的时候把不属于发布内容的部分排除掉。
如何在生产环境里面排除开发环境的 目录、类文件 的方法??
先认识:不同编译器
SPring boot
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Maven编译器:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
无论是SpringBoot or Maven编译器,设置排除项目的方式差不多。
在configuration > excludes 下面添加排除匹配条件。
** 表示多级目录匹配。
* 表示一级目录匹配。
后处理:
处理方式上面有截图和说明,这里不拖堂。附上我的整个pom.xml,也不算过分,参考用:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.huawei</groupId> <artifactId>hwapi</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hwapi</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-all</artifactId> <version>3.0.84</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <!– <scope>test</scope> –> </dependency>--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> <exclusion> <artifactId>guava</artifactId> <groupId>com.google.guava</groupId> </exclusion> <!--<exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion>--> </exclusions> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jetbrains/annotations --> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>13.0</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <!-- 跳过测试代码的编译 --> <skip>true</skip> <!--排除的--> <excludes> <exclude>**/hwapi/**</exclude> <exclude>**/TestMain.java</exclude> </excludes> </configuration> </plugin> <!-- 单元测试插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <!-- 跳过整个单元测试的执行 --> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
4.总结:
1.maven打包过程一定看console,盯着问题。
2. 先clean 在package 。
3.问题不大,高手碰见该问题直接掠过该文章。完毕。