iT邦幫忙

2023 iThome 鐵人賽

DAY 12
0
Modern Web

來架個網站吧系列 第 12

來架個網站吧-12.網站開發-2: 設定環境參數-1

  • 分享至 

  • xImage
  •  
tags: 來架個網站吧 Grails

我是目錄


昨天可以開啟專案之後,新增最主要的工作內容就是把專案連上資料庫

安裝開發資料庫

安裝

  • 資料庫版本: postgresql 15

因為 postgresql 官方網站已經有詳細的安裝步驟,在這邊就不贅述。

postgresql 下載網址: https://www.postgresql.org/download/

開資料庫

安裝完成資料庫之後,就可以建立資料庫,指令如下:

  • 進入資料庫
sudo -u postgres psql
  • 執行sql
-- 新增資料庫
CREATE DATABASE ironman_dict_db;

-- 新增資料庫角色
CREATE ROLE dict_dba NOINHERIT;

-- 授予 dict_dba 角色可連線至 ironman_dict_db 資料庫
GRANT CONNECT ON DATABASE ironman_dict_db TO dict_dba;

-- 授予 dict_dba 在 DATABASE 所有權限
GRANT ALL ON DATABASE ironman_dict_db to dict_dba;

-- 新增 dict_ap 使用者
CREATE USER dict_ap WITH PASSWORD 'LF2.net';

-- 把 dict_ap (user role) 加入 dict (group role) 中
GRANT dict_ap TO dict_dba;

設定專案環境參數

確認專案可以正常部屬之後,接下就是針對當前的專案環境修改內容。主要修改下列文件

  • gradle.properties
  • build.gradle
  • grails-app/conf/application.yml

gradle.properties

grailsVersion=5.3.3
grailsGradlePluginVersion=5.3.0
groovyVersion=3.0.11
gorm.version=7.3.3
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.jvmargs=-Dfile.encoding=UTF-8 -Xmx1024M
springBootVersion=2.7.13
hibernateCore=5.6.15.Final
  • grailsVersion: 當前的grails版本
  • grailsGradlePluginVersion: 當前的Grails Gradle Plugin版本,通常與 grailsVersion 一致
  • groovyVersion: groovy 版本,建議只升級小版本號,跨中間版本號有可能出現can't not find symbol。
  • gorm.version: Grails GORM 版本,建議只升級小版本號。
  • springBootVersion: 這個參數是我自行新增的,主要是為了更新 spring Boot,建議只升級小版本號。
  • hibernateCore: 這個參數是我自行新增的,主要是為了更新 hibernate Core,建議只升級小版本號。

build.gradle

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsGradlePluginVersion"
        classpath "gradle.plugin.com.github.erdi.webdriver-binaries:webdriver-binaries-gradle-plugin:2.6"
        classpath "org.grails.plugins:hibernate5:7.3.0"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:3.4.7"
    }
}

version "0.1"
group "ironman.dict"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"com.github.erdi.webdriver-binaries"
apply plugin:"org.grails.grails-gsp"
apply plugin:"com.bertramlabs.asset-pipeline"

repositories {
    mavenCentral()
    maven { url "https://repo.grails.org/grails/core" }
}

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    compileOnly "io.micronaut:micronaut-inject-groovy"
    console "org.grails:grails-console"
    implementation "org.springframework.boot:spring-boot-starter-logging:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-validation:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-autoconfigure:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-tomcat:${springBootVersion}"
    implementation "org.grails:grails-core"
    implementation "org.grails:grails-web-boot"
    implementation "org.grails:grails-logging"
    implementation "org.grails:grails-plugin-rest"
    implementation "org.grails:grails-plugin-databinding"
    implementation "org.grails:grails-plugin-i18n"
    implementation "org.grails:grails-plugin-services"
    implementation "org.grails:grails-plugin-url-mappings"
    implementation "org.grails:grails-plugin-interceptors"
    implementation "org.grails.plugins:cache"
    implementation "org.grails.plugins:async"
    implementation "org.grails.plugins:scaffolding"
    implementation "org.grails.plugins:hibernate5"
    implementation "org.hibernate:hibernate-core:${hibernateCore}"
    implementation "org.grails.plugins:events"
    implementation "org.grails.plugins:gsp"
    profile "org.grails.profiles:web"
    runtimeOnly "org.glassfish.web:el-impl:2.2.1-b05"
    runtimeOnly "org.apache.tomcat:tomcat-jdbc"
    runtimeOnly "javax.xml.bind:jaxb-api:2.3.1"
    runtimeOnly "com.bertramlabs.plugins:asset-pipeline-grails:3.4.7"
    testImplementation "io.micronaut:micronaut-inject-groovy"
    testImplementation "org.grails:grails-gorm-testing-support"
    testImplementation "org.mockito:mockito-core"
    testImplementation "org.grails:grails-web-testing-support"
    testImplementation "org.grails.plugins:geb"
    testImplementation "org.seleniumhq.selenium:selenium-remote-driver:4.0.0"
    testImplementation "org.seleniumhq.selenium:selenium-api:4.0.0"
    testImplementation "org.seleniumhq.selenium:selenium-support:4.0.0"
    testRuntimeOnly "org.seleniumhq.selenium:selenium-chrome-driver:4.0.0"
    testRuntimeOnly "org.seleniumhq.selenium:selenium-firefox-driver:4.0.0"

    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.6.0'

    //hikaricp
    implementation "com.zaxxer:HikariCP:5.0.1"
}

bootRun {
    ignoreExitValue true
    jvmArgs(
        '-Dspring.output.ansi.enabled=always', 
        '-noverify', 
        '-XX:TieredStopAtLevel=1',
        '-Xmx1024m')
    sourceResources sourceSets.main
    String springProfilesActive = 'spring.profiles.active'
    systemProperty springProfilesActive, System.getProperty(springProfilesActive)
}

tasks.withType(GroovyCompile) {
    configure(groovyOptions) {
        forkOptions.jvmArgs = ['-Xmx1024m']
    }
}

tasks.withType(Test) {
    useJUnitPlatform()
}

webdriverBinaries {
    if (!System.getenv().containsKey('GITHUB_ACTIONS')) {
        chromedriver {
            version = '2.45.0'
            fallbackTo32Bit = true
        }
        geckodriver '0.30.0'
    }
}

tasks.withType(Test) {
    systemProperty "geb.env", System.getProperty('geb.env')
    systemProperty "geb.build.reportsDir", reporting.file("geb/integrationTest")
    if (!System.getenv().containsKey('GITHUB_ACTIONS')) {
        systemProperty 'webdriver.chrome.driver', System.getProperty('webdriver.chrome.driver')
        systemProperty 'webdriver.gecko.driver', System.getProperty('webdriver.gecko.driver')
    } else {
        systemProperty 'webdriver.chrome.driver', "${System.getenv('CHROMEWEBDRIVER')}/chromedriver"
        systemProperty 'webdriver.gecko.driver', "${System.getenv('GECKOWEBDRIVER')}/geckodriver"
    }
}


assets {
    minifyJs = true
    minifyCss = true
}

grails 專案所有中的 plugin 都是在這邊設定。另外,初始化的 gradle.properties 文件,預設會有 H2 相關套件記得移除。


上一篇
來架個網站吧-11.網站開發-1: 建立專案
下一篇
來架個網站吧-13.網站開發-3: 設定環境參數-2
系列文
來架個網站吧30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言