簡體   English   中英

Maven spring-boot:運行目標:簡化命令行

[英]Maven spring-boot:run goal : Simplify the command line

我需要簡化一個丑陋且沒有優雅的命令行:

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 ,我只需要在spring.application.json config參數上發送配置。

我不認為使用屬性文件是否可以解決。

有任何想法嗎?

您使用Spring Boot方式的外部化配置,配置具有環境變量系統屬性作為源:

來自SPRING_APPLICATION_JSON的屬性(嵌入在環境變量或系統屬性中的嵌入式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):

mvn -pl rep-digital-api spring-boot:run 
-Dspring-boot.run.arguments=--spring.config.additional-location=classpath:/foo.properties 

您可以為此在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>

然后您的命令行將變為mvn -pl rep-digital-api spring-boot:run -P local

我使用類似的設置,但是調用了一個不同的插件。 我可能沒有在此代碼示例中正確設置配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM