2018年第50周-maven脚手架: jc-spring-boot-rest-archetype

功能

此archetype(脚手架)用于快速生成基于spring-boot, mybatis的RESTful Web Service项目。
虽然Spring给出SpringBoot的RESTful Web Service项目例子,但仅仅是控制层端,数据层并未给出。需要用上数据层的话,还需要研究下。最近想快速的做个网页搜索引擎,缺一个web界面,于是决定我将我所研究的记录下,做成一个archetype,以后就可以快速生成REST接口项目,生成的项目是扫描Mapper,读取数据层数据,返回RESTfull数据。
REST接口可用于前后端分离项目,前端的脚手架我推荐使用飞冰,可快速生成vue前端页面项目。

前期准备

  • Linux操作系统(最好,因为方便运行shell脚本)
  • 安装maven,3.5版本以上
  • jdk8
  • git

安装过程

下载jc-spring-boot-rest-archetype脚手架源码,进入根目录执行执行以下命令安装

git clone https://gitee.com/jevondata/jc-spring-boot-rest-archetype.git
mvn install

使用jc-spring-boot-rest-archetype脚手架生成代码并运行

在shell命令行里,随便进入一个新的目录,执行maven的命令:

mvn archetype:generate

执行过程如下:

always@always-PC:/mydata/test$ mvn archetype:generate
Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> com.jc.archetype:jc-spring-boot-rest-archetype (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Downloading from jc-spring-boot-rest-archetype-repo: http://nexus.jevoncode.com/nexus/content/groups/public/com/jc/archetype/jc-spring-boot-rest-archetype/0.0.1.0-SNAPSHOT/maven-metadata.xml
Downloaded from jc-spring-boot-rest-archetype-repo: http://nexus.jevoncode.com/nexus/content/groups/public/com/jc/archetype/jc-spring-boot-rest-archetype/0.0.1.0-SNAPSHOT/maven-metadata.xml (800 B at 12 kB/s)
Define value for property 'groupId': com.jc.web
Define value for property 'artifactId': jc-web
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.jc.web: :
Confirm properties configuration:
groupId: com.jc.web
artifactId: jc-web
version: 1.0-SNAPSHOT
package: com.jc.web
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: jc-spring-boot-rest-archetype:0.0.1.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.jc.web
[INFO] Parameter: artifactId, Value: jc-web
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.jc.web
[INFO] Parameter: packageInPathFormat, Value: com/jc/web
[INFO] Parameter: package, Value: com.jc.web
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.jc.web
[INFO] Parameter: artifactId, Value: jc-web
[INFO] Project created from Archetype in dir: /mydata/test/jc-web
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  37.535 s
[INFO] Finished at: 2018-12-16T01:54:17+08:00
[INFO] ------------------------------------------------------------------------

此过程会让你选择groupId, artifactId, version, package,根据自己所需选择输入即可,最后选择Y,即生成以下代码:

always@always-PC:/mydata/test$ tree
.
└── jc-web
    ├── pom.xml
    └── src
        └── main
            ├── assembly                    --maven-assembly-plugin插件用的配置文件,绑定在pacakge阶段
            │   └── dep.xml
            ├── environment                 --打包时用的环境参数
            │   ├── development.properties
            │   └── test.properties
            ├── java                        --项目Java代码结构采用典型的项目结构, controller, service, mapper, entity
            │   └── com
            │       └── jc
            │           └── web
            │               ├── App.java
            │               ├── dto
            │               │   └── JcPage.java
            │               ├── entity
            │               │   └── JcMyInfo.java
            │               ├── exception
            │               │   └── JcMyInfoException.java
            │               ├── GreetingController.java
            │               ├── Greeting.java
            │               ├── mapper
            │               │   ├── JcMyInfoMapper.java
            │               │   └── JcMyInfoMapper.xml
            │               └── service
            │                   ├── JcMyInfoServiceImpl.java
            │                   └── JcMyInfoService.java
            ├── resources
            │   ├── application.properties   --spring-boot相关的配置文件
            │   ├── data.sql                 --启动时初始化数据库测试数据
            │   ├── logback.xml              --日志配置文件
            │   ├── schema.sql               --启动时初始化数据库测试表结构
            │   └── system.properties
            └── scripts                      --打包后,用于启动项目的脚本,可配置JVM内存等信息
                ├── env.sh
                ├── service.sh
                ├── shutdown.sh
                └── startup.sh

16 directories, 23 files

生成的代码可以直接编译打包:

always@always-PC:/mydata/test$ cd jc-web/
always@always-PC:/mydata/test/jc-web$ mvn package

打包完后,解压文件,执行startup.sh即可启动服务

always@always-PC:/mydata/test/jc-web$ cp target/jc-web-1.0-SNAPSHOT-development-bin.tar.gz ../
always@always-PC:/mydata/test/jc-web$ cd ../
always@always-PC:/mydata/test$ tar -zxf jc-web-1.0-SNAPSHOT-development-bin.tar.gz
always@always-PC:/mydata/test$ cd jc-web-1.0-SNAPSHOT-development/
always@always-PC:/mydata/test/jc-web-1.0-SNAPSHOT-development$ ./bin/startup.sh
startup.sh based at /mydata/test/jc-web-1.0-SNAPSHOT-development
service.sh based at /mydata/test/jc-web-1.0-SNAPSHOT-development
/gucci/jdk1.8.0_191/bin/java -Xmx4048m -Xms256m -server -cp /mydata/test/jc-web-1.0-SNAPSHOT-development/conf:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/antlr-2.7.7.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/aspectjweaver-1.9.2.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/byte-buddy-1.9.5.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/classmate-1.4.0.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/dom4j-2.1.1.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/h2-1.4.197.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/hibernate-commons-annotations-5.0.4.Final.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/hibernate-core-5.3.7.Final.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/hibernate-validator-6.0.13.Final.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/HikariCP-3.2.0.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jackson-annotations-2.9.0.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jackson-core-2.9.7.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jackson-databind-2.9.7.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jackson-datatype-jdk8-2.9.7.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jackson-datatype-jsr310-2.9.7.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jackson-module-parameter-names-2.9.7.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jandex-2.0.5.Final.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/javassist-3.23.1-GA.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/javax.activation-api-1.2.0.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/javax.annotation-api-1.3.2.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/javax.persistence-api-2.2.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/javax.transaction-api-1.3.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jaxb-api-2.3.1.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jboss-logging-3.3.2.Final.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jc-web-1.0-SNAPSHOT.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/jul-to-slf4j-1.7.25.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/log4j-api-2.11.1.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/log4j-to-slf4j-2.11.1.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/logback-classic-1.2.3.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/logback-core-1.2.3.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/mybatis-3.4.6.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/mybatis-spring-1.3.2.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/mybatis-spring-boot-autoconfigure-1.3.2.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/mybatis-spring-boot-starter-1.3.2.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/slf4j-api-1.7.25.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/snakeyaml-1.23.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-aop-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-aspects-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-beans-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-autoconfigure-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-aop-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-data-jpa-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-jdbc-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-json-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-logging-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-tomcat-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-boot-starter-web-2.1.1.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-context-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-core-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-data-commons-2.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-data-jpa-2.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-expression-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-jcl-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-jdbc-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-orm-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-tx-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-web-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/spring-webmvc-5.1.3.RELEASE.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/tomcat-embed-core-9.0.13.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/tomcat-embed-el-9.0.13.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/tomcat-embed-websocket-9.0.13.jar:/mydata/test/jc-web-1.0-SNAPSHOT-development/lib/validation-api-2.0.1.Final.jar:  com.jc.web.App
nohup: redirecting stderr to stdout

查看日志文件

always@always-PC:/mydata/test/jc-web-1.0-SNAPSHOT-development$ tail -f logs/server.log
12-16 13:15:39 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
12-16 13:15:39 [main] INFO  org.apache.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read
12-16 13:15:40 [main] INFO  org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
12-16 13:15:40 [main] INFO  com.jc.web.App - Started App in 6.167 seconds (JVM running for 6.797)
12-16 13:15:40 [main] INFO  com.jc.web.App - started Spring Boot

访问接口http://localhost:8080/, 即返回以下信息:

{"id":1,"content":"Hello, This is jevoncode, and my blog is https://segmentfault.com/u/huangxiaoshu/articles"}

BINGO! 到这里RESTful Web Service项目就生成好了。

简单说明

pom模板使用jdk8,编码格式都是utf-8
数据库用的是H2内存数据库,所以可根据自己需求修改成mysql等。
此脚手架用了都是我自己的本地私库, 所以编译安装时需自行修改nexus地址或删掉。

在eclipse添加remote catalogs:

Window ——> Preferences ——> Maven ——> Archetypes ——> Add Remote Catalog
Catalog File: http://nexus.jevoncode.com/ne…
Description: nexus.jevoncode.com

在idea添加remote catalogs:

New ——> Project ——> Maven ——> Add Archetypes

    原文作者:黄小数
    原文地址: https://segmentfault.com/a/1190000017391957
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞