1. 程式人生 > >用SpringBoot搭建聚合專案實戰記錄

用SpringBoot搭建聚合專案實戰記錄

  1. 建立聚合父工程
    File - New - Project - Maven - [Next] - GroupId(格式:cn.hnlp2es.parent),ArtifactId(格式:hnlp2es-parent) - 一路Next
    說明:父工程主要是統一定義jar包的版本。以下是最終的porm.
<?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>cn.hnlp2es.parent</groupId> <artifactId>hnlp2es-parent</artifactId> <packaging>pom</packaging> <version
>
1.0-SNAPSHOT</version> <modules> <module>hnlp2es-web</module> <module>hnlp2es-service</module> <module>hnlp2es-repo</module> <module>hnlp2es-entity</module> <module>hnlp2es-myhanlp</module>
</modules> <name>hnlp2es</name> <description>hanlp to elasticsearch</description> <!-- parent指明繼承關係,給出被繼承的父專案的具體資訊 繼承說明:這裡繼承SpringBoot提供的父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 集中定義依賴版本號 --> <properties> <junit.version>4.12</junit.version> <!--<spring.version>4.2.4.RELEASE</spring.version>--> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.32</mysql.version> <slf4j.version>1.6.4</slf4j.version> <!--<jackson.version>2.4.2</jackson.version>--> <druid.version>1.0.9</druid.version> <httpclient.version>4.3.5</httpclient.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <joda-time.version>2.5</joda-time.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <commons-net.version>3.3</commons-net.version> <pagehelper.version>3.7.5</pagehelper.version> <jsqlparser.version>0.9.1</jsqlparser.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <jedis.version>2.7.2</jedis.version> <solrj.version>4.10.3</solrj.version> <dubbo.version>2.5.3</dubbo.version> <zookeeper.version>3.4.7</zookeeper.version> <zkclient.version>0.1</zkclient.version> <activemq.version>5.11.2</activemq.version> <freemarker.version>2.3.23</freemarker.version> <quartz.version>2.2.2</quartz.version> <spring-boot-dependencies.version>1.5.8.RELEASE</spring-boot-dependencies.version> </properties> <dependencyManagement> <dependencies> <!-- 時間操作元件 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> <!-- Apache工具元件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>${commons-net.version}</version> </dependency> <!-- Jackson Json處理工具包 --> <!--<dependency>--> <!--<groupId>com.fasterxml.jackson.core</groupId>--> <!--<artifactId>jackson-databind</artifactId>--> <!--<version>${jackson.version}</version>--> <!--</dependency>--> <!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpclient.version}</version> </dependency> <!-- quartz任務排程框架 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>${quartz.version}</version> </dependency> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 日誌處理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>${mybatis.paginator.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- Spring --> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-context</artifactId>--> <!--<version>${spring.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-beans</artifactId>--> <!--<version>${spring.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-webmvc</artifactId>--> <!--<version>${spring.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-jdbc</artifactId>--> <!--<version>${spring.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-aspects</artifactId>--> <!--<version>${spring.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-jms</artifactId>--> <!--<version>${spring.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-context-support</artifactId>--> <!--<version>${spring.version}</version>--> <!--</dependency>--> <!-- JSP相關 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!-- 檔案上傳元件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <!-- Redis客戶端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <!-- solr客戶端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency> <!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 資原始檔拷貝外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java編譯外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.8.RELEASE</version> <configuration> <mainClass>MmWebApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> <!--<pluginManagement>--> <!--<plugins>--> <!--&lt;!&ndash; 配置Tomcat外掛 &ndash;&gt;--> <!--<plugin>--> <!--<groupId>org.apache.tomcat.maven</groupId>--> <!--<artifactId>tomcat7-maven-plugin</artifactId>--> <!--<version>2.2</version>--> <!--</plugin>--> <!--</plugins>--> <!--</pluginManagement>--> </build> </project>

2 . 定義maven路徑
File - Settings - Build,Execution,Deployment_Bulid Tools_Maven
Override 其中的settings file為本地maven的 settings.xml 地址。
然後Local repository 會根據settings.xml中的倉庫的地址而變化。
然後讓上面的pom.xml檔案生效。[Import Changes]

3 . 建立模組工程Common
File - New - Module - Maven - Parent選父工程,但是仍然將 Add as module to 置為 none - 照上面規則填好 GroupId,ArtifactId - 專案名稱為 hnlp2es-common 後 一路Next
說明:將common模組放在parent同個目錄下(雖然不知道為啥)

<?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>hnlp2es-parent</artifactId>
        <groupId>cn.hnlp2es.parent</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../hnlp2es-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hnlp2es-common</artifactId>


</project>

4 . 建立子模組工程 web,service,repo,entity
對著父工程右鍵 - New - Module - > 輸入 hnlp2es-web
對著父工程右鍵 - New - Module - > 輸入 hnlp2es-service
對著父工程右鍵 - New - Module - > 輸入 hnlp2es-repo
對著父工程右鍵 - New - Module - > 輸入 hnlp2es-entity
其中:
web的pom.xml
- 用jar啟動,於是packaging為jar.

<?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>hnlp2es-parent</artifactId>
        <groupId>cn.hnlp2es.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.hnlp2es.web</groupId>
    <artifactId>hnlp2es-web</artifactId>
    <!--  add 打包方式  -->
    <packaging>jar</packaging>

    <!--  add web模組相關依賴  -->
    <dependencies>
        <dependency>
            <groupId>cn.hnlp2es.service</groupId>
            <artifactId>hnlp2es-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>cn.hnlp2es.entity</groupId>
            <artifactId>hnlp2es-entity</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--  add sb相關依賴  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

</project>

service 的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>hnlp2es-parent</artifactId>
        <groupId>cn.hnlp2es.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.hnlp2es.entity</groupId>
    <artifactId>hnlp2es-entity</artifactId>
    <packaging>jar</packaging>

</project>

repo的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>hnlp2es-parent</artifactId>
        <groupId>cn.hnlp2es.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.hnlp2es.repo</groupId>
    <artifactId>hnlp2es-repo</artifactId>


</project>

entity的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>hnlp2es-parent</artifactId>
        <groupId>cn.hnlp2es.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.hnlp2es.entity</groupId>
    <artifactId>hnlp2es-entity</artifactId>
    <packaging>jar</packaging>

</project>

5 .編碼測試
主要在web中進行編碼
這裡寫圖片描述
java 目錄下建立 com.sbaggre Package,然後在該包下新建 ApplicationStarter.java 檔案;
在 com.sbaggre 下新建controller目錄,然後在該目錄下新建 HelloController.java 檔案.

package com.sbaggre;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApplicationStarter {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationStarter.class, args);
    }
}
package com.sbaggre.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello/")
public class HelloController {

    @GetMapping("say")
    public String sayHello(){
        return "Hello World.";
    }
}

在這個過程中踩過的坑:
1-專案間依賴jar包的問題:
報錯:Maven Error : Could not resolve dependencies for project
具體報錯:Failed to collect dependencies for [com.xxx:jar:1.0 (compile),… -> [Help 1]

-1-確認pom.xml依賴沒有寫錯;
-2-開啟右邊的Maven外掛,看看有木有報錯,專案裡有木有灰色的(灰色的需要點亮-Unignore–參考:https://www.cnblogs.com/jimisun/p/9059728.html
-3-關於被引用的模組專案,並不是非packaging為jar

2-springboot與spring引用的衝突
報錯:Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc….
應該是版本的衝突
-1-我的解決方法是將pom.xml中的spring相關的依賴註釋掉。

3-左邊的 External Libraries 只有jdk,沒有pom.xml中引用的包。
一般情況下,專案建好後External Libraries 就會自動新增pom中的依賴包。並且會隨著pom的改變而變更。
所以可能原因是過程中maven出錯了導致流程錯誤。
這個問題,也導致了不能在編碼中利用pom中的jar包!
-1-調整pom檔案中的錯誤,我的是web專案的packaging原為war,而應該是jar.
-2-maven 的clean - install.當子模組的install有錯誤的時候,將parent進行install嘗試下。

4-Spring-boot啟動
開啟頁面顯示報錯:Whitelabel Error Page
-知識點-通過psvm的方法,程式只能載入Application.java所在包及其子包下的內容
而且,Application.java 檔案不能直接放在main/java資料夾下,必須要建一個包把他放進去
看來得多去官網看看!
-1-(參考:https://www.cnblogs.com/JealousGirl/p/whitelabel.html)有比較全的解決方案。
-2-(參考:https://segmentfault.com/a/1190000004493460)這裡有另一個報錯

5-jar方式啟動Spring-boot報錯:springboot jar啟動沒有主清單屬性
-1-在parent的pom中新增spring-boot-maven-plugin即可解決(參考:https://www.cnblogs.com/niceboat/p/6230448.html

好像是這些坑吧。