用SpringBoot搭建聚合项目实战记录

  1. 创建聚合父工程
    File - New - Project - Maven - [Next] - GroupId(格式:cn.hnlp2es.parent),ArtifactId(格式:hnlp2es-parent) - 一路Next
    说明:父工程主要是统一定义jar包的版本。以下是最终的porm.

<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.0modelVersion>

    <groupId>cn.hnlp2es.parentgroupId>
    <artifactId>hnlp2es-parentartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>hnlp2es-webmodule>
        <module>hnlp2es-servicemodule>
        <module>hnlp2es-repomodule>
        <module>hnlp2es-entitymodule>
        <module>hnlp2es-myhanlpmodule>
    modules>

    <name>hnlp2esname>
    <description>hanlp to elasticsearchdescription>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.8.RELEASEversion>
        <relativePath/>
        
    parent>

    
    <properties>
        <junit.version>4.12junit.version>
        
        <mybatis.version>3.2.8mybatis.version>
        <mybatis.spring.version>1.2.2mybatis.spring.version>
        <mybatis.paginator.version>1.2.15mybatis.paginator.version>
        <mysql.version>5.1.32mysql.version>
        <slf4j.version>1.6.4slf4j.version>
        
        <druid.version>1.0.9druid.version>
        <httpclient.version>4.3.5httpclient.version>
        <jstl.version>1.2jstl.version>
        <servlet-api.version>2.5servlet-api.version>
        <jsp-api.version>2.0jsp-api.version>
        <joda-time.version>2.5joda-time.version>
        <commons-lang3.version>3.3.2commons-lang3.version>
        <commons-io.version>1.3.2commons-io.version>
        <commons-net.version>3.3commons-net.version>
        <pagehelper.version>3.7.5pagehelper.version>
        <jsqlparser.version>0.9.1jsqlparser.version>
        <commons-fileupload.version>1.3.1commons-fileupload.version>
        <jedis.version>2.7.2jedis.version>
        <solrj.version>4.10.3solrj.version>
        <dubbo.version>2.5.3dubbo.version>
        <zookeeper.version>3.4.7zookeeper.version>
        <zkclient.version>0.1zkclient.version>
        <activemq.version>5.11.2activemq.version>
        <freemarker.version>2.3.23freemarker.version>
        <quartz.version>2.2.2quartz.version>
        <spring-boot-dependencies.version>1.5.8.RELEASEspring-boot-dependencies.version>
    properties>
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>joda-timegroupId>
                <artifactId>joda-timeartifactId>
                <version>${joda-time.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-lang3artifactId>
                <version>${commons-lang3.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-ioartifactId>
                <version>${commons-io.version}version>
            dependency>
            <dependency>
                <groupId>commons-netgroupId>
                <artifactId>commons-netartifactId>
                <version>${commons-net.version}version>
            dependency>
            
            
                
                
                
            
            
            <dependency>
                <groupId>org.apache.httpcomponentsgroupId>
                <artifactId>httpclientartifactId>
                <version>${httpclient.version}version>
            dependency>
            
            <dependency>
                <groupId>org.quartz-schedulergroupId>
                <artifactId>quartzartifactId>
                <version>${quartz.version}version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>${slf4j.version}version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>${mybatis.version}version>
            dependency>
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>${mybatis.spring.version}version>
            dependency>
            <dependency>
                <groupId>com.github.miemiedevgroupId>
                <artifactId>mybatis-paginatorartifactId>
                <version>${mybatis.paginator.version}version>
            dependency>
            <dependency>
                <groupId>com.github.pagehelpergroupId>
                <artifactId>pagehelperartifactId>
                <version>${pagehelper.version}version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>${druid.version}version>
            dependency>
            
            
                
                
                
            
            
                
                
                
            
            
                
                
                
            
            
                
                
                
            
            
                
                
                
            
            
                
                
                
            
            
                
                
                
            
            
            <dependency>
                <groupId>jstlgroupId>
                <artifactId>jstlartifactId>
                <version>${jstl.version}version>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>servlet-apiartifactId>
                <version>${servlet-api.version}version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jsp-apiartifactId>
                <version>${jsp-api.version}version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>commons-fileuploadgroupId>
                <artifactId>commons-fileuploadartifactId>
                <version>${commons-fileupload.version}version>
            dependency>
            
            <dependency>
                <groupId>redis.clientsgroupId>
                <artifactId>jedisartifactId>
                <version>${jedis.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.solrgroupId>
                <artifactId>solr-solrjartifactId>
                <version>${solrj.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>dubboartifactId>
                <version>${dubbo.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
                <version>${zookeeper.version}version>
            dependency>
            <dependency>
                <groupId>com.github.sgroschupfgroupId>
                <artifactId>zkclientartifactId>
                <version>${zkclient.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.activemqgroupId>
                <artifactId>activemq-allartifactId>
                <version>${activemq.version}version>
            dependency>
            <dependency>
                <groupId>org.freemarkergroupId>
                <artifactId>freemarkerartifactId>
                <version>${freemarker.version}version>
            dependency>
            <dependency>
                
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring-boot-dependencies.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <finalName>${project.artifactId}finalName>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
                <version>2.7version>
                <configuration>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.2version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>1.5.8.RELEASEversion>
                <configuration>
                    <mainClass>MmWebApplicationmainClass>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>

        plugins>
        
            
                
                
                    
                    
                    
                
            
        
    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同个目录下(虽然不知道为啥)


<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-parentartifactId>
        <groupId>cn.hnlp2es.parentgroupId>
        <version>1.0-SNAPSHOTversion>
        <relativePath>../hnlp2es-parent/pom.xmlrelativePath>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>hnlp2es-commonartifactId>


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.


<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-parentartifactId>
        <groupId>cn.hnlp2es.parentgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.hnlp2es.webgroupId>
    <artifactId>hnlp2es-webartifactId>
    
    <packaging>jarpackaging>

    
    <dependencies>
        <dependency>
            <groupId>cn.hnlp2es.servicegroupId>
            <artifactId>hnlp2es-serviceartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>cn.hnlp2es.entitygroupId>
            <artifactId>hnlp2es-entityartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


    dependencies>

project>

service 的pom.xml


<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-parentartifactId>
        <groupId>cn.hnlp2es.parentgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.hnlp2es.entitygroupId>
    <artifactId>hnlp2es-entityartifactId>
    <packaging>jarpackaging>

project>

repo的pom.xml


<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-parentartifactId>
        <groupId>cn.hnlp2es.parentgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.hnlp2es.repogroupId>
    <artifactId>hnlp2es-repoartifactId>


project>

entity的pom.xml文件


<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-parentartifactId>
        <groupId>cn.hnlp2es.parentgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.hnlp2es.entitygroupId>
    <artifactId>hnlp2es-entityartifactId>
    <packaging>jarpackaging>

project>

5 .编码测试
主要在web中进行编码
用SpringBoot搭建聚合项目实战记录_第1张图片
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)


好像是这些坑吧。

你可能感兴趣的:(java)