Jhipster为我们提供了实体的相关命令,但是从图中可以看到并没有删除实体的操作

file

而且我们再次输入:yo jhipster:entity User 时,提供的选项中也没有清除这一选项

file

这也就意味着,我们将手动删除这个实体相关的所有文件(entity: domain, repository, rest, test, Angularjs controller & services, HTML view, link in menu, HTML view),可能还将面临清理不彻底的问题。

一旦我们清理不彻底,下次更新这个微服务时,我们删除的这个实体又会自动生成 那么如何才能彻底删除?我在国外的一个网站上终于找到了一种方式,那就是脚本删除,命令如下:

#!/usr/bin/env bash
echo;
if [ -z "$1" ];
then
    printf "Required argument GROUP_ID你输入的 GROUP_ID 是 is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME.\n";
    exit 1;
else
    GROUP_ID=$1;
    echo "你输入的 GROUP_ID 是 '$1'.";
fi
if [ -z "$2" ];
then
    printf "Required argument ENTITY_NAME is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME .\n";
    exit 1;
else
    ENTITY_NAME=$2;
    JAVA_ENTITY_NAME=echo ${ENTITY_NAME:0:1} | tr  '[a-z]' '[A-Z]'${ENTITY_NAME:1}
    echo "你输入的实体名称是 '$2'."
    echo "即将删除您的实体相关信息:";
    echo "domain, repository, rest, test, Angularjs controller & services, HTML view, link in menu, HTML view";
fi
JAVA_ENTITY_NAME=echo ${ENTITY_NAME:0:1} | tr  '[a-z]' '[A-Z]'${ENTITY_NAME:1}
UNDERSCORED_FOLDER_NAME=echo ${ENTITY_NAME} | sed -r 's/([a-z0-9])([A-Z])/\1-\L\2/g'
QUESTION=$'如果你想在将来重新生成实体,你可以选择保留实体自定义配置文件(.jhipster/'${JAVA_ENTITY_NAME}'.json)\n你想删除实体定义文件吗?(yes or no)'
while true; do
    read -p "${QUESTION}" yn
    case $yn in
        [Yy]* ) rm -rf ./.jhipster/${ENTITY_NAME}.json; break;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done
echo;
echo "Starting to delete files...";
rm -rf src/main/resources/config/liquibase/changelog/*_added_entity_${JAVA_ENTITY_NAME}.xml
rm -rf src/main/java/${GROUP_ID}domain/${JAVA_ENTITY_NAME}.java
rm -rf src/main/java/${GROUP_ID}repository/${JAVA_ENTITY_NAME}Repository.java
rm -rf src/main/java/${GROUP_ID}service/${JAVA_ENTITY_NAME}Service.java
rm -rf src/main/java/${GROUP_ID}service/${JAVA_ENTITY_NAME}QueryService.java
rm -rf src/main/java/${GROUP_ID}service/impl/${JAVA_ENTITY_NAME}ServiceImpl.java
rm -rf src/main/java/${GROUP_ID}repository/search/${JAVA_ENTITY_NAME}SearchRepository.java
rm -rf src/main/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}Resource.java
rm -rf src/main/java/${GROUP_ID}web/rest/dto/${JAVA_ENTITY_NAME}DTO.java
rm -rf src/main/java/${GROUP_ID}web/rest/dto/${JAVA_ENTITY_NAME}Criteria.java
rm -rf src/main/java/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}Mapper.java
rm -rf target/generated-sources/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}MapperImpl.java
rm -rf src/main/webapp/app/entities/${UNDERSCORED_FOLDER_NAME}/*
rm -rf src/test/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}ResourceIntTest.java
rm -rf src/test/gatling/simulations/${JAVA_ENTITY_NAME}GatlingTest.scala
rm -rf src/test/javascript/spec/app/entities/${UNDERSCORED_FOLDER_NAME}/*
rm -rf src/test/javascript/spec/app/entities/${UNDERSCORED_FOLDER_NAME}
rm -rf src/main/webapp/i18n/en/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/fr/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/ru/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/uz/${ENTITY_NAME}.json
echo "Deleting entity '${ENTITY_NAME}' is completed.";
echo;
echo "-----------------------------------------------------";
echo "Do not forget to manually correct these files also:  ";
echo "-----------------------------------------------------";
echo " --> src/main/webapp/index.html"
echo " --> src/main/webapp/scripts/compopnents/navbar.html"
echo " --> src/main/webapp/i18n/**/global.json"
echo " --> src/main/resources/config/liquibase/master.xml"
echo " --> src/main/resources/config/mongeez/master.xml"

复制该代码,保存delete-entity.sh,文件后缀为.sh,我们将这个文件置于这个项目的根目录中,只需要执行:
delete-entity.sh [package]/ [entityName]
如:delete-entity.sh me/zhengjie/ User

注意:
package在这里不能使用.,不然会找不到文件,需要使用/,最后也需要加上/ 弹出如下窗口,输入yes,即可彻底删除

file

注意
有些东西是需要手动更新的,如:

src/main/webapp/index.html
src/main/webapp/scripts/compopnents/navbar.html
src/main/webapp/i18n/**/global.json
src/main/resources/config/liquibase/master.xml (if you use liquibase)
src/main/resources/config/mongeez/master.xml   (if you use mongodb)

重启项目,发现报错

file

进入目录,src/main/resources/config/liquibase/master.xml 删除文件中报错的行即可。

最后修改:2019 年 10 月 19 日 12 : 01 PM