0

我有一个 mavenized build (maven3),它有一个加载 mySQL 数据库转储的模块。这在 Windows 下可以正常工作,但在 Linux 下不行。

使用 -X 开关运行 maven,它会生成以下输出:

[DEBUG] Using mysql at /usr/bin/mysql
[DEBUG] Command to execute: mysql -uMYUSER --password=**** -hlocalhost --quick --max_allowed_packet=16M --default-character-set=utf8 -e "source /this/path/to/my/database/dump/is/longer/than/70/characters/the_dump.sql"
[ERROR] ERROR 1102 (42000): Incorrect database name '/this/path/to/my/database/dump/is/longer/than/70/characters/the_dump.'

如果我直接在命令行上运行调试输出,the_dump.sql 脚本工作正常。我的猜测是 Maven 没有正确识别参数,和/或截断它们(请注意,第二个参数在错误消息中被截断为 70 个字符)。我无法承受在 Windows 下构建失败 - 由于数据库脚本中的某些命令,它无法通过使用“<”管道将其传递到 mysql 来正确运行。

这是相关的 pom 片段:

<build>
    <resources>
        <resource>
            <directory>META-INF</directory>
            <targetPath>META-INF</targetPath>
        </resource>
        <resource>
            <directory>${dumps.target.dir}</directory>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>my.maven.plugin</groupId>
            <artifactId>db-dumps-maven-plugin</artifactId>

            <configuration>
                <databasesToDump>${db.schemas.to.dump}</databasesToDump>
                <documentStoreFolder>${documentstore.location}</documentStoreFolder>
                <outputDirectory>${project.build.directory}/../${dumps.target.dir}</outputDirectory>
            </configuration>

            <executions>
                <execution>
                    <id>dump-database</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>dump-db</goal>
                    </goals>
                    <inherited>false</inherited>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>build</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>my.maven.plugin</groupId>
                    <artifactId>db-dumps-maven-plugin</artifactId>

                    <executions>
                        <execution>
                            <id>create-database</id>
                            <phase>initialize</phase>
                            <goals>
                                <goal>create-db</goal>
                            </goals>
                            <inherited>false</inherited>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

以及来自 db-dumps-maven-plugin 的执行片段:

@Override
public void execute() throws MojoExecutionException {

    final Set<String> dumpsToInsertLowerCase = evaluateDumpsToInsert();

    final List<File> sqlFiles = findFiles(folderWithDumps, ".sql", dumpsToInsertLowerCase);
    getLog().info("Inserting dumps: " + sqlFiles);

    for (final File sqlFile : sqlFiles) {
        String createCommand = "mysql -u%s --password=%s -h%s --quick --max_allowed_packet=16M --default-character-set=utf8 -e \"source %s\"";
        Object[] createArgs = {username, password, hostname, sqlFile.getAbsolutePath().replace("\\", "/")};
        executeCommand(createCommand, createArgs);
    }
}

public void executeCommand(final String command, final Object[] arguments) {

    final String commandToExecute = String.format(command, arguments);
    mavenPlugin.getLog().debug("Command to execute: " + commandToExecute);

    try {
        final Process child = Runtime.getRuntime().exec(commandToExecute);

        final InputStream inputStream = child.getInputStream();
        readAndCloseStream(inputStream, false);

        final InputStream errorStream = child.getErrorStream();
        readAndCloseStream(errorStream, true);

        // prevent leaking file descriptors, close stream
        try {
            child.getOutputStream().close();
        } catch (final IOException ioe) {
            mavenPlugin.getLog().warn("Unable to close stream. " + ioe);
        }

        // check return value
        // 0 means a normal program termination
        final int exitValue = child.exitValue();
        if (exitValue != 0) {
            throw new RuntimeException("Executing command failed. Exit code: " + exitValue);
        }

    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

知道为什么会发生这种情况,以及如何让 Maven 正确解释源命令吗?

4

0 回答 0