引用地址https://www.cnblogs.com/geekdc/p/10744903.html
一、問題描述:
項目分多個模塊,open-eureka注冊中心、open-provider服務提供者、open-common公共部分,provider依賴common。父pom使用spring-boot-maver-plugin插件,項目直接運行Main主類沒問題,但是install報common中的類找不到符號.
二、查找問題:
spring-boot-maven-plugin 打包跟普通的apache-maven-plugin打包不一致,前者打的jar 包是可以直接用java -jar name.jar 來執行的,但是common模塊只是作為一個其他模塊的依賴來使用,並不需要有啟動類,也不需要執行。
三、解決辦法:
3.1、刪除父pom中的spring-boot-maven-plugin插件依賴,父pom不需要<build>
parent.pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.starfast.open</groupId> <artifactId>starfast-open</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>open-eureka</module> <module>open-provider</module> <module>open-feign</module> <module>open-common</module> </modules> <packaging>pom</packaging> <name>starfast-open</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> <witown.open.version>1.0-SNAPSHOT</witown.open.version> <open.common.version>0.0.1-SNAPSHOT</open.common.version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> </dependencies> <dependencyManagement> <!--引入spring-cloud依賴--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.witown.app</groupId> <artifactId>open-sdk</artifactId> <version>${witown.open.version}</version> </dependency> <dependency> <groupId>com.starfast.open</groupId> <artifactId>open-common</artifactId> <version>${open.common.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
3.2、只在需要獨立運行的模塊,如provider模塊中加載spring-boot-maven-plugin插件依賴
provider.pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>starfast-open</artifactId> <groupId>com.starfast.open</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>open-provider</artifactId> <name>open-provider</name> <dependencies> <!--common模塊--> <dependency> <groupId>com.starfast.open</groupId> <artifactId>open-common</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.3、刪除不需要獨立運行的模塊中的spring-boot-maven-plugin插件依賴
common.pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>starfast-open</artifactId> <groupId>com.starfast.open</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>open-common</artifactId> <packaging>jar</packaging> <name>open-common</name> <dependencies> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--開放平台--> <dependency> <groupId>com.witown.app</groupId> <artifactId>open-sdk</artifactId> </dependency> <!--監控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--單元測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
install結果:
順便吐槽,網上的垃圾文章真多,還好發現了該篇文章,
https://blog.csdn.net/SnailMann/article/details/81710461
多謝作者。
ps:實際操作中,我原來項目就是在parent中定義了build,並且在所依賴的本項目中的jar包中使用了build
1.去掉parent中的build
2.去掉所依賴的本項目中的模塊pom中的build部分(也解決了不需要發布的jar包,而只是被別的項目依賴的jar包,還需要寫main入口程序的無用代碼)
3.在需要發布的項目模塊中,添加build,並且格式為
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork : 如果沒有該項配置devtools不會起作用,即應用不會restart --> <fork>true</fork> <addResources>true</addResources><!--支持靜態文件熱部署 --> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/webapp</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
4.注意要把資源文件也用build打包