用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.";
    }
}

6 . 启动验证
直接右键绿色三角形启动。
打开 http://localhost:8080/hello/say
显示 Hello World.
说明成功。


参考网址:
SpringBoot多模块项目实践(Multi-Module)
https://segmentfault.com/a/1190000011367492#articleHeader3
Spring Boot 多模块项目创建与配置 (一)
https://www.cnblogs.com/MaxElephant/p/8205234.html
IntelliJ IDEA教程之Maven工程多模块继承和聚合创建(详解)
http://www.marsitman.com/idea/intellij-idea-create-maven-union.html


在这个过程中踩过的坑:
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


好像是这些坑吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值