Maven spring引导:运行目标:简化命令行

Maven spring引导:运行目标:简化命令行,spring,maven,spring-boot,Spring,Maven,Spring Boot,我需要简化一个丑陋而不优雅的命令行: mvn -pl rep-digital-api spring-boot:run -Dspring.application.json='{ "server.port": 8081, "spring": { "datasource": { "url": "jdbc:postgresql://localhost:5432/frontoffice", "username": "frontoffice", "password": "password", "driver

我需要简化一个丑陋而不优雅的命令行:

mvn -pl rep-digital-api spring-boot:run -Dspring.application.json='{ "server.port": 8081, "spring": { "datasource": { "url": "jdbc:postgresql://localhost:5432/frontoffice", "username": "frontoffice", "password": "password", "driverClassName": "org.postgresql.Driver" }, "redis": { "host": "localhost", "port": 6379 } }, "api.url-oauth": "http://localhost:8081/oauth/token", "url-ui": "http://localhost:3000", "document.upload-dir":"/home/jcabre/data/upload", "document.pending-key":"server3-pending" }'
正如您所知,我只需要在
spring.application.json
config参数上发送配置

我不知道使用属性文件是否是一种解决方案

有什么想法吗?

您使用一种方法,该方法以环境变量或系统属性作为源:

SPRING_应用程序_JSON的属性(嵌入到 环境变量或系统属性)

因此,作为替代方案,您可以将该命令包装在一个bash/sh脚本中,该脚本为env变量赋值并运行spring boot,但实际上可读性并不高:

SPRING_APPLICATION_JSON = '{ "server.port": 8081, "spring": { "datasource": { "url": "jdbc:postgresql://localhost:5432/frontoffice", "username": "frontoffice", "password": "password", "driverClassName": "org.postgresql.Driver" }, "redis": { "host": "localhost", "port": 6379 } }, "api.url-oauth": "http://localhost:8081/oauth/token", "url-ui": "http://localhost:3000", "document.upload-dir":"/home/jcabre/data/upload", "document.pending-key":"server3-pending" }'        
mvn -pl rep-digital-api spring-boot:run 
作为JSON内联的另一种选择,您还可以将JSON作为JNDI变量提供,如下所示:
java:comp/env/spring.application.JSON.
,但是使用JDNI来实现这一点似乎有点复杂,而且不够直观

事实上,你正在评估许多财产。因此,使用外部化配置文件读取/更新它们似乎更好。 我建议您在属性或YAML外部文件中定义它们:

mvn -pl rep-digital-api spring-boot:run 
-Dspring-boot.run.arguments=-spring.config.location=classpath:/foo.properties 
或者使用
spring.config.additional location
添加而不是覆盖默认位置(从spring Boot 2开始):

您使用的方法是将环境变量或系统属性作为源:

SPRING_应用程序_JSON的属性(嵌入到 环境变量或系统属性)

因此,作为替代方案,您可以将该命令包装在一个bash/sh脚本中,该脚本为env变量赋值并运行spring boot,但实际上可读性并不高:

SPRING_APPLICATION_JSON = '{ "server.port": 8081, "spring": { "datasource": { "url": "jdbc:postgresql://localhost:5432/frontoffice", "username": "frontoffice", "password": "password", "driverClassName": "org.postgresql.Driver" }, "redis": { "host": "localhost", "port": 6379 } }, "api.url-oauth": "http://localhost:8081/oauth/token", "url-ui": "http://localhost:3000", "document.upload-dir":"/home/jcabre/data/upload", "document.pending-key":"server3-pending" }'        
mvn -pl rep-digital-api spring-boot:run 
作为JSON内联的另一种选择,您还可以将JSON作为JNDI变量提供,如下所示:
java:comp/env/spring.application.JSON.
,但是使用JDNI来实现这一点似乎有点复杂,而且不够直观

事实上,你正在评估许多财产。因此,使用外部化配置文件读取/更新它们似乎更好。 我建议您在属性或YAML外部文件中定义它们:

mvn -pl rep-digital-api spring-boot:run 
-Dspring-boot.run.arguments=-spring.config.location=classpath:/foo.properties 
或者使用
spring.config.additional location
添加而不是覆盖默认位置(从spring Boot 2开始):


您可以为此在
pom.xml
中定义一个概要文件,并使用
properties maven plugin
,但您需要一个没有更改的默认概要文件和这个概要文件

<profiles>
    <profile>
        <id>deflt</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <activeprofile>deflt</activeprofile>
        </properties>
    </profile>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <activeprofile>local</activeprofile>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.0.0</version>
                    <executions>
                        <execution>
                            <id>local-run</id>
                            <configuration>
                                <properties>
                                    <spring.application.json>{ "server.port": 8081, "spring": { "datasource": { "url": "jdbc:postgresql://localhost:5432/frontoffice", "username": "frontoffice", "password": "password", "driverClassName": "org.postgresql.Driver" }, "redis": { "host": "localhost", "port": 6379 } }, "api.url-oauth": "http://localhost:8081/oauth/token", "url-ui": "http://localhost:3000", "document.upload-dir":"/home/jcabre/data/upload", "document.pending-key":"server3-pending" }</spring.application.json>
                                </properties>
                            </configuration>
                            <goals>
                                <goal>set-system-properties</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

解除
真的
解除
地方的
假的
地方的
org.codehaus.mojo
属性maven插件
1.0.0
本地经营
{“server.port”:8081,“spring”:{“datasource”:{“url”:“jdbc:p”ostgresql://localhost:5432/frontoffice“,”用户名“:”frontoffice“,”密码“:”密码“,”driverClassName“:”org.postgresql.Driver“}”,redis“{”主机“:”本地主机“,”端口“:”6379}}”,api.url oauth:”http://localhost:8081/oauth/token,“url用户界面”:http://localhost:3000", “document.upload dir”:“/home/jcabre/data/upload”,“document.pending key”:“server3 pending”}
设置系统属性
然后您的命令行变成
mvn-pl rep digital api spring boot:run-p local


我使用了类似的设置,但调用了不同的插件。我可能没有在此代码示例中正确设置配置。

为此,您可以在
pom.xml
中定义配置文件,并使用
properties maven plugin
,但您需要一个没有更改的默认配置文件和此配置文件

<profiles>
    <profile>
        <id>deflt</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <activeprofile>deflt</activeprofile>
        </properties>
    </profile>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <activeprofile>local</activeprofile>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.0.0</version>
                    <executions>
                        <execution>
                            <id>local-run</id>
                            <configuration>
                                <properties>
                                    <spring.application.json>{ "server.port": 8081, "spring": { "datasource": { "url": "jdbc:postgresql://localhost:5432/frontoffice", "username": "frontoffice", "password": "password", "driverClassName": "org.postgresql.Driver" }, "redis": { "host": "localhost", "port": 6379 } }, "api.url-oauth": "http://localhost:8081/oauth/token", "url-ui": "http://localhost:3000", "document.upload-dir":"/home/jcabre/data/upload", "document.pending-key":"server3-pending" }</spring.application.json>
                                </properties>
                            </configuration>
                            <goals>
                                <goal>set-system-properties</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

解除
真的
解除
地方的
假的
地方的
org.codehaus.mojo
属性maven插件
1.0.0
本地经营
{“server.port”:8081,“spring”:{“datasource”:{“url”:“jdbc:p”ostgresql://localhost:5432/frontoffice“,”用户名“:”frontoffice“,”密码“:”密码“,”driverClassName“:”org.postgresql.Driver“}”,redis“{”主机“:”本地主机“,”端口“:”6379}}”,api.url oauth:”http://localhost:8081/oauth/token,“url用户界面”:http://localhost:3000", "document.upload dir:“/home/jcabre/data/upload”,“document.pending key:“server3 pending”}
设置系统属性
然后您的命令行变成
mvn-pl rep digital api spring boot:run-p local


我使用了类似的设置,但调用了不同的插件。在这个代码示例中,我可能没有正确设置配置。

如果在Linux上运行,您可以使用
alias
。我忘记了Windows等价物是什么,但我相当肯定有一个。如果在Linux上运行,您可以使用
alias
。我忘记了Windows eEquivalent是,但我相当肯定有一个。有可能引用本地文件而不是类路径文件吗?我的意思是,我想我在
/foo.proerties
上有一个文件。当然。如果属性位于应用程序所在的位置