无法通过我的 spring maven 项目集成和运行 liquibase

Posted

技术标签:

【中文标题】无法通过我的 spring maven 项目集成和运行 liquibase【英文标题】:Not able to integrate and run liquibase through my spring maven project 【发布时间】:2017-10-14 18:59:39 【问题描述】:

我正在尝试通过基于弹簧的maven 项目运行mvn liquibase:update。但我看到以下错误。

错误:

$ mvn liquibase:update
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=386m; support was removed in 8.0
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building liquibase-samples 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- liquibase-maven-plugin:3.0.5:update (default-cli) @ liquibase-samples ---
[INFO] ------------------------------------------------------------------------
[INFO] Parsing Liquibase Properties File
[INFO]   File: src/main/resources/liquibase/liquibase.properties
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:postgresql://gsi-547576:5432/emp-db
INFO 5/15/17 4:30 PM:liquibase: null: null: Successfully acquired change log lock
SEVERE 5/15/17 4:30 PM:liquibase: null: null: cvc-elt.1: Cannot find the declaration of element 'databaseChangeLog'.
INFO 5/15/17 4:30 PM:liquibase: null: null: Successfully released change log lock
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.225 s
[INFO] Finished at: 2017-05-15T16:30:09-04:00
[INFO] Final Memory: 14M/1011M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.0.5:update (default-cli) on project liquibase-samples: Error setting up or running Liquibase: Error parsing line 5 column 91 of src/main/resources/liquibase/db-changelog-master.xml: cvc-elt.1: Cannot find the declaration of element 'databaseChangeLog'. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

这是我的集成方式。

pom.xml

<?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.study</groupId>
    <artifactId>liquibase-samples</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1102-jdbc41</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.0.5</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase/liquibase.properties</propertyFile>
                    <changeLogFile>src/main/resources/liquibase/db-changelog-master.xml</changeLogFile>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>update</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

src/main/resources/liquibase/liquibase.properties

driver=org.postgresql.Driver
url=jdbc:postgresql://localhost:5432/emp-db
username=abc
password=abc

src/main/resources/liquibase/db-changelog-master.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                            http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
        <include file="liquibase/db-changelog-1.0.xml" relativeToChangelogFile="true"/> 
    </databaseChangeLog>

src/main/resources/liquibase/db-changelog-1.0.xml

<?xml version="1.0" encoding="utf-8" ?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

    <changeSet id="create_department" author="chandeln">
        <createTable tableName="department">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false" />
            </column>
        </createTable>
    </changeSet>

    <changeSet id="create_employee" author="chandeln">
        <createTable tableName="employee">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="emp_name" type="varchar(50)">
                <constraints nullable="false" />
            </column>
            <column name="dept" type="int"/>
        </createTable>
    </changeSet>

    <changeSet id="tag-1.0" author="sheng.w">
        <tagDatabase tag="1.0" />
    </changeSet>

</databaseChangeLog>

【问题讨论】:

【参考方案1】:

下面的帖子解决了我的问题。

Maven build failing with Liquibase: cvc-elt.1 declaration of element cannot be found

依赖版本和插件版本都应该匹配。我的新 pom 如下所示。

pom.xml

<dependencies>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1102-jdbc41</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.5.3</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase/liquibase.properties</propertyFile>
                    <changeLogFile>src/main/resources/liquibase/db-changelog-master.xml</changeLogFile>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>update</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

【讨论】:

以上是关于无法通过我的 spring maven 项目集成和运行 liquibase的主要内容,如果未能解决你的问题,请参考以下文章

MAVEN BUILD FAILURE - 无法解析项目 XYZ 的依赖关系

使用 Maven 进行集成测试的最佳实践?

无法使用 Maven 编译多模块 Spring 项目

使用 Spring 在多模块 Maven 项目中进行集成测试

无法在我的 Spring Boot 项目中使用 Maven 依赖项

如何将 Spring Boot 项目集成到已经存在的多模块 Maven 项目中?