脚手架就相当于是一个模板,若我们采用脚手架,则可以重用之前的代码实现快速开发。
在Activiti的目录结构(见下图)中,原本就是带有脚手架的,但默认不能使用,需要我们手动安装。今天就给大家介绍如何安装脚手架。下面以Activiti系列学习的上一篇文章【Activiti系列学习6】中的Helloworld程序为例来创建一个自定义的脚手架。
一、创建脚手架
1.切换分支
由于我们的项目是从github上克隆编译而来,通常会存在一个分支,而那个分支并非我们关注的重点,此时我们需要切换分支。我们打开IDEA中的终端,然后以activiti-6.0.0这个分支分化出一个我们需要的分支,取名为study6(我这显示已经存在了,可以直接切换到study6上)。
git checkout study6
2.安装脚手架
切换好分支后,进入tooling/archetypes目录,进行安装。
mvn clean install
以上已经成功安装了默认的脚手架,只需要进行后面的第6步,即可添加这个自带的脚手架(名称中把2去掉)。
下面介绍添加一个以二级审批流程(我们的Helloworld程序)为例的自定义的脚手架。
3.拷贝脚手架
拷贝activiti-archetype-unittest目录,重命名为 activiti-archetype-unittest2,并修改pom.xml中的内容为activiti-archetype-unittest2
4.修改 activiti-archetype-unittest2内容
(1)在archetype-resources/src目录下创建目录man/java和main/resources(默认只有test/java和test/resources目录)
(2)将Helloworld项目中的内容拷贝过来
java中的DemoMain.java,并修改文件中的包名为package ${package};并添加其他三个文件(activiti.cfg.xml,logback.xml,second_approve.bpmn20.xml)到resources文件夹中。需要文件者可从以下地址自取。
链接:https://pan.baidu.com/s/1VmeKjionnhNkzCi6p4Kktg
提取码:dnyv
(3)修改 META-INF/maven/archetype-metadata.xml文件中的四个fileSet,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="activiti-archetype-unittest2">
<fileSets>
<fileSet filtered="true" packaged="true">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="false" packaged="false">
<directory>src/main/resources</directory>
<includes>
<include>activiti.cfg.xml</include>
<include>logback.xml</include>
<include>second_approve.bpmn20.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true">
<directory>src/test/resources</directory>
<includes>
<include>my-process.bpmn20.xml</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
filetered是否过滤,packaged是否经过包结构过滤,两个都是true;之前的包结构占位符能够替换
(4)删除不用的文件
删除掉test/resource下的activiti.cfg.xml和log4j.properties,因为我们采用logback.xml。删除时把search in comments and strings前面的钩去掉。
5.安装自定义脚手架
进入到本地的activiti-archetype-unittest2目录中进行安装
cd activiti-archetype-unittest2
mvn clean install
6.添加脚手架
将我们安装好的脚手架添加到脚手架目录中
7.显示添加成功
二、使用脚手架
1.创建项目
重新建一个maven项目(此时不用脚手架),取名为activiti6-samples,GroupId可取com.syc.activiti,ArtifactId可取activiti6-samples。然后新建一个module,选择Maven,并且从archetype创建,选择我们建好的unittest2,模块名为config。
2.导入依赖
在项目的pom.xml中修改parent为springboot,因为helloword程序依赖的是springboot。
<?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.syc.activiti</groupId>
<artifactId>activiti6-samples</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>config</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath></relativePath>
</parent>
</project>
在config的pom.xml中添加guava依赖,不添加该依赖会报错,最好将原先的依赖全部导入
<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.syc.activiti</groupId>
<artifactId>config</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>6.0.0</version>
<exclusions>
<exclusion>
<artifactId>activation</artifactId>
<groupId>javax.activation</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>
</project>
3.maven中Unignore Projects
点击右侧Maven中的Unignore Projects,然后点击刷新
刷新项目后就是可运行状态
运行项目后出现警告:
WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/D:/maven_repository/org/mybatis/mybatis/3.4.2/mybatis-3.4.2.jar) to method java.lang.Object.finalize()
解决方法:该问题是jdk的版本过高,我们将project structure中的jdk设置回1.8即可。
修改正确后即可正常运行程序