环境变量中的数据库名称

时间:2019-09-16 12:54:15

标签: java spring maven spring-boot jooq

我的问题如下,我有一个Spring启动应用程序,并且对SQL使用Jooq,我将pom设置为生成表。数据库信息在环境变量中设置。 在Intellij中它可以正常工作,但是当我生成.WAR并将该应用程序上载到客户端时,只有在客户端数据库名称与创建jooq代码时客户端数据库名称与pom.xml中的名称相同的情况下,它才有效。 这些表完全相同,唯一可以更改的是数据库名称,因此我在环境变量中查找名称信息。 我的变量全部在属性中设置,在调试中,我可以看到带有变量数据的连接对象。

我的pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0

<groupId>com.speedsoft</groupId>
<artifactId>bedelServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--    <packaging>jar</packaging>-->
<packaging>war</packaging>

<name>BWS</name>
<description>BWS</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <java.version>1.8</java.version>
    <quartz.version>2.2.1</quartz.version>
    <jooq.version>3.9.1</jooq.version>
    <logback.version>1.2.3</logback.version>
    <logbackaccess.version>1.3.0-alpha4</logbackaccess.version>
    <mysql.version>6.0.3</mysql.version>
    <exec-maven-plugin>1.7</exec-maven-plugin>

    <start-class>com.speedsoft.bws.BwsApplication</start-class>
</properties>



<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>${quartz.version}</version>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>${quartz.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq</artifactId>
        <version>${jooq.version}</version>
    </dependency>

    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-meta</artifactId>
        <version>${jooq.version}</version>
    </dependency>

    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen</artifactId>
        <version>${jooq.version}</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>2.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-access</artifactId>
        <version>${logbackaccess.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.0.1</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <version>3.8.3</version>

            <!-- The plugin should hook into the generate goal -->
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>

            <dependencies/>

            <configuration>
                <jdbc>
                    <driver>${jdbc.driver}</driver>
                    <url>${jdbc.url}</url>
                    <user>${jdbc.user}</user>
                    <password>${jdbc.password}</password>
                </jdbc>

                <generator>
                    <database>
                        <inputSchema>bedel</inputSchema>
                        <name>org.jooq.util.mysql.MySQLDatabase</name>
                    </database>
                    <target>
                        <packageName>com.speedsoft.bws.jooq.model</packageName>
                        <directory>target/generated-sources/jooq</directory>
                        <encoding>UTF-8</encoding>
                    </target>
                    <generate>
                    <fluentSetters>false</fluentSetters>
                    <daos>false</daos>
                    </generate>
                </generator>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.5.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <systemProperties>
                    <systemProperty>
                        <key>jdbc.driver</key>
                        <value>${jdbc.driver}</value>
                    </systemProperty>
                    <systemProperty>
                        <key>jdbc.user</key>
                        <value>${jdbc.user}</value>
                    </systemProperty>
                    <systemProperty>
                        <key>jdbc.password</key>
                        <value>${jdbc.password}</value>
                    </systemProperty>
                    <systemProperty>
                        <key>jdbc.url</key>
                        <value>${jdbc.url}</value>
                    </systemProperty>
                </systemProperties>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <jdbc.user>root</jdbc.user>
            <jdbc.password>%47**¨00</jdbc.password>
            <jdbc.url>jdbc:mysql://localhost:3306/bedel?serverTimezone=UTC</jdbc.url>
            <jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
        </properties>
    </profile>
</profiles>

我如何配置我的pom.xml,以便在生成.WAR时不会插入对具有与代码生成时间相同名称的数据库的依赖?

我是否必须在模式中输入所有可能的名称?

<schemata>
  <schema>
    <inputSchema>ui</inputSchema>
  </schema>
  <schema>
    <inputSchema>other_schema</inputSchema>
  </schema>
</schemata>

1 个答案:

答案 0 :(得分:1)

读取环境变量

You can read environment variables directly with Maven通过使用${env.VARIABLE_NAME}语法。如果您知道只有两种模式,但是它们的具体名称可能会因环境而异,则可以这样写:

<schemata>
  <schema>
    <inputSchema>${env.UI_SCHEMA}</inputSchema>
  </schema>
  <schema>
    <inputSchema>${env.OTHER_SCHEMA}</inputSchema>
  </schema>
</schemata>

使用程序代码生成配置

总是有可能实现编程代码生成配置,这将允许您对输入模式进行更动态的配置。在此处记录:https://www.jooq.org/doc/latest/manual/code-generation/codegen-programmatic

使用运行时模式映射

您可以选择在版本控制中签入已生成的代码(有优缺点),而不是根据环境重新生成代码,并使用运行时模式映射功能在运行时修补生成的模式: https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-render-mapping/