我有一个mavenized构建(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正确解释源命令?