<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 簡介 在開發的過程中,經常需要面對不同的運行環境(開發環境、測試環境、生產環境、內網環境、外網環境等等),在不同的環境中,相關的配置一般不一樣,比如數據源配置、日志文件配置、以及一些軟件運行過程中的基本配置。每次在不同環境部署程序時,都需要修改相應的配置文件,使之完成環境的配置。 # 項目目錄 ![](https://img.kancloud.cn/9d/20/9d2096d7f0a8aec8eb7422d20c2bfe36_323x165.png) # 代碼 `jdbc.properties` ~~~ jdbc.driver=${jdbc_driver} jdbc.url=${jdbc_url} jdbc.username=${jdbc_username} jdbc.password=${jdbc_password} ~~~ `filter-dev-env.properties` ~~~ jdbc_driver=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://127.0.0.1:3306/dev jdbc_username=root jdbc_password=123456 ~~~ 其他類似,只是庫不一樣 `pom.xml` ~~~xml <profiles> <profile> <!-- 本地開發環境 --> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> <!-- 默認是本地開發環境 --> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 測試環境 --> <id>test</id> <properties> <profiles.active>test</profiles.active> </properties> </profile> <profile> <!-- 生產環境 --> <id>pro</id> <properties> <profiles.active>pro</profiles.active> </properties> </profile> </profiles> <build> <!-- maven模塊化的話最好從父類繼成取,打成包的命名 --> <finalName>${artifactId}-${version}</finalName> <!-- 使用指定的filter進行過濾,在執行mvn命令的時候帶上-Ppro就代表生產環境,就會加載生產環境的properties,-Pdev就代表開發環境(默認) --> <filters> <filter>src/main/resources/multiEnv/filter-${profiles.active}-env.properties</filter> </filters> <!-- 配置需要被替換的資源文件路徑, jdbc.properties --> <resources> <resource> <!-- 資源文件位置src/main/resources/,這下面的資源文件的${}會全部被替換成filter中的標簽內容。 directory指定的value會作為classes的資源跟目錄, 比如指定:src/main/resources/,則classes下會出現jdbc等包, 若指定:src/main/resources/jdbc/,則classes下直接出現jdbc包下的文件,不會額外出現jdbc等其他包結構。因為他把jdbc作為了根目錄 --> <directory>src/main/resources/</directory> <!-- 在某個resource中如果設置filtering為true,將會根據輸入參數動態修改相關內容。只有我們開啟資源過濾后,在?properties?文件中才能引用我們在?pom?文件中定義的屬性 --> <filtering>true</filtering> <!-- 排除標簽 --> <excludes> <!-- exclude可以排除指定文件,支持通配符 ,匹配項不會生成到classes目錄下,路徑是以directory開始的 在這里就是directory(src/main/resources/)/multiEnv/filter-*-env.properties --> <exclude>multiEnv/filter-*-env.properties</exclude> <!-- **/*.xml 代表 directory(src/main/resources/)目錄以及所有子目錄的xml文件--> <!-- <exclude>**/*.xml</exclude> <exclude>**/*.properties</exclude> --> </excludes> <!-- 包含標簽 --> <!-- <includes> <include></include> </includes> --> </resource> </resources> </build> ~~~ # 運行 打包時,自己肯定知道是生產環境還是部署環境,所以只需要在打包的時候加上參數即可,如下: **打本地開發環境包**:`mvn clean package -Dmaven.test.skip=true -Pdev` **打部署上線環境包**:`mvn clean package -Dmaven.test.skip=true -Ppro` **打測試環境包**:`mvn clean package -Dmaven.test.skip=true -Ptest` # 另一個例子 application.properties ~~~ app=learn-maven-properties version=1.0.0 ## 下面信息有maven profile動態設置 apiUrl=${maven-apiUrl} username=${maven-username} password=${maven-password} ~~~ pom.xml ~~~ <!-- 定義maven的版本屬性 --> <properties> <spring.version>4.3.12.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <finalName>learn-maven-properties</finalName> <!-- 必須開啟資源過濾 --> <!-- 只有我們開啟資源過濾后,在 properties 文件中才能引用我們在 pom 文件中定義的屬性 --> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> </build> <profiles> <profile> <id>default</id> <properties> <!-- 自定義屬性 --> <maven-apiUrl>http://192.168.10.180:8080</maven-apiUrl> <maven-username>root</maven-username> <maven-password>aaaaaa</maven-password> </properties> <activation> <!-- 設置默認激活這個配置 --> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>release</id> <properties> <!-- 自定義屬性 --> <maven-apiUrl>http://10.87.11.228:8080</maven-apiUrl> <maven-username>root</maven-username> <maven-password>relase_password</maven-password> </properties> </profile> </profiles> </project> ~~~ 上面的 pom.xml 文件定義了兩個profile,分別為 default 和 release;每個profile中定義了三個屬性,分別為“maven-apiUrl”、“maven-username”和“maven-password”屬性,這些屬性將在 application.properties 屬性文件中通過“${}”方式使用。 `mvn -D`代表(Properties屬性) \-D參數和執行 java 時指定的 -D 類似。使用命令行設置屬性 -D 的正確方法是: ~~~ mvn -DpropertyName=propertyValue clean package ~~~ 其中: * 如果 propertyName 不存在 pom.xml,它將被設置。 * 如果 propertyName 已經存在 pom.xml,其值將被作為參數傳遞的值覆蓋-D。 要發送多個變量,請使用多個空格分隔符加-D: ~~~ mvn -DpropA=valueA -DpropB=valueB -DpropC=valueC clean package ~~~ 執行 ~~~ mvn?-D?maven-password=123456?clean?package ~~~ 使用 `-D` 修改 maven-password 的值為 123456 --- \-P代表(Profiles配置文件) 在上面的 pom.xml 文件中,指定的分別為“default”和“release”,可以通過-P進行傳遞“default”或“release”來激活某個 profile。運行如下命令: ~~~ mvn -Prelease clean package ~~~ --- 使用activation標簽指定其他的激活條件 ~~~ <profiles> <profile> <id>default</id> <activation> <property> <name>env</name> <value>dev</value> </property> <!-- 設置默認激活這個配置 --> <activeByDefault>true</activeByDefault> </activation> <properties> <!-- 自定義屬性 --> <maven-apiUrl>http://192.168.10.180:8080</maven-apiUrl> <maven-username>root</maven-username> <maven-password>aaaaaa</maven-password> </properties> </profile> <profile> <id>release</id> <activation> <property> <name>env</name> <value>release</value> </property> </activation> <properties> <!-- 自定義屬性 --> <maven-apiUrl>http://10.87.11.228:8080</maven-apiUrl> <maven-username>root</maven-username> <maven-password>relase_password</maven-password> </properties> </profile> </profiles> ~~~ 上面在每個profile中定義了一個激活條件,即 env 屬性;當 env=dev 時,激活 id=default 的 profile;當env=release,激活 id=release 的 profile。 ~~~ mvn -D env=release clean package ~~~ 將激活屬性 env=release 的 profile 這里不要使用“`mvn -P env=release clean package`”,我們定義的 profile 觸發條件為屬性 env 等于 “dev”或“release”,使用 -P 將拋出提示信息: ~~~ [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.556 s [INFO] Finished at: 2019-08-07T08:55:30+08:00 [INFO] ------------------------------------------------------------------------ [WARNING] The requested profile "env=release" could not be activated because it does not exist. ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看