外部で設定ファイルを変更することができますMavenのパッケージャを使用する方法

6ヶ月過去の物事は非常に複雑で、長い時間がないよりボーに到達します。この2日間は、Javaの開発、非常にイライラ経験の問題、ちょうど将来の参照のために記録を模索し始めて。問題は、データベースにXMLファイルの情報に遭遇され、XMLファイルはかなり特殊で、データベースに対応するラベルのフィールド名が同じではありませんが、別の設定ファイルを維持する必要性は、私は、JSONの設定ファイルにそれを開催します直接後者を変更します。最近のJava関連のコンテンツでだけ見て、私はあなたの手を練習するためにJavaを使用していました。achieve'dする機能はすぐに一日のためにどのようにこの動的プロファイルカードの結果を得ることができます。

問題の説明

リソースファイル(コンフィグレーションファイル)Mavenのパッケージは、Mavenのアセンブリ、プラグインパッケージ化ジャーにパッケージングされない用途には、手動で修正することができる方法

解決プロセス

段ピット1:SqlServerのドライバの問題

次のようにドライブに直接依存して与えられたのSQLServerのMavenリポジトリサイト:

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>7.4.1.jre11</version>
    <scope>test</scope>
</dependency>

コースでする必要がある<scope>text</scope>テスト、私はこのホワイトピットを設計したときにそれ以外の場合はこれが唯一の追加依存し、削除

ディレクトリのリソースファイルを取得します。

ファイルのパスを取得するには、注目を集めるために、次の方法で提供されたリソースディレクトリが異なる環境で異なります

private static String getBasePath() {
    // 该函数在不同环境下获得的路径是不同的
    // 编译环境下得到的路径是 .../target/classes/
    // 打包成 jar 文件后,得到的路径是 jar 文件的位置
    // 此处获得的路径,即使在 windows 下,使用的也是 linux 下的文件分隔符
    String basePath = AppConfig.class.getProtectionDomain().getCodeSource().getLocation().getPath();

    // 如果包含中文路径,则对其进行 decode 处理
    basePath = URLDecoder.decode(basePath, StandardCharsets.UTF_8);

    // 将路径中的文件分割符更换为当前运行环境的文件分隔符
    basePath = basePath.replace('/', System.getProperty("file.separator").charAt(0));

    // 在打包环境下,取得 jar 文件所在的文件夹路径,而不是 jar 文件路径
    int firstIndex = basePath.indexOf(System.getProperty("file.separator")) + 1;
    int lastIndex = basePath.lastIndexOf(System.getProperty("file.separator")) + 1;
    basePath = basePath.substring(firstIndex, lastIndex);

    // 设定配置文件目录,结尾带文件分隔符
    basePath = basePath + "config" + System.getProperty("file.separator");
    return basePath;
    }

pom.xmlの元のリソースファイルを除外

<build>
    ...
    <resources>
        <!-- 排除默认资源文件 -->
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
    </resources>
    ...
</build>

コピーの設定ファイルにコンパイルする前に、

コンフィギュレーションファイルのコンパイル時のコピーtarget/class使用するためのフォルダで指定されたファイル(ここで設定フォルダがある)、プログラムは直接によってここで実行されたときにmaven-resources-pluginプラグイン

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- 绑定到 maven 生命周期的哪一节段 -->
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <!-- ${project.build.outputDirectory} 为构建过程输出目录,缺省为target/classes -->
                        <outputDirectory>${project.build.outputDirectory}/config</outputDirectory>
                        <resources>
                            <resource>
                                <!-- 需要拷贝的资源文件位置 -->
                                <directory>src/main/resources</directory>
                                <!-- 开启变量替换,将 pom.xml 中的相关变量替换至 properties 文件中,该项目中未使用该特性 -->
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

リソースファイルのパッケージのコピー

のでmaven-assembly-plugin、プラグ、一つのpom.xml構成され、二つの部分に設けられている、構成がassembly.xmlあります

  • pom.xml
<build>
    <plugins>
        ...
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <!-- 配置描述符文件 -->
                <appendAssemblyId>true</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <!-- 将组装绑定到maven生命周期的哪一阶段 -->
                    <phase>package</phase>
                    <goals>
                        <!-- 指定assembly插件的打包方式 -->
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        ...
    </plugins>
</build>
  • assembly.xml
<assembly>
    ...
    <fileSets>
        ...
        <!-- 对资源文件进行打包 -->
        <fileSet>
            <!-- ${project.build.outputDirectory} 为构建过程输出目录,缺省为 target/classes -->
            <directory>${project.build.outputDirectory}/config</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        ...
    </fileSets>
    ...
</assembly>

他の

外部依存関係の設定ではなく、Benpianフォーカス、ここでは説明しない、pom.xmlファイルとassembly.xmlとして完全なファイルもあります。

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.4.1.jre11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <target>11</target>
                    <source>11</source>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <!-- ${project.build.outputDirectory} 为构建过程输出目录,缺省为target/classes -->
                            <outputDirectory>${project.build.outputDirectory}/config</outputDirectory>
                            <resources>
                                <resource>
                                    <!-- 需要拷贝的资源文件位置 -->
                                    <directory>src/main/resources</directory>
                                    <!-- 开启变量替换,将 pom.xml 中的相关变量替换至 properties 文件中,该项目中未使用该特性 -->
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>cn.scodi.catia_xml2db.ApplicationRunner</mainClass>
                        </manifest>
                    </archive>
                    <!--过滤掉不希望包含在jar中的文件-->
                    <excludes>
                        <!-- 排除不需要的文件夹(路径是jar包内部的路径) -->
                        <exclude>**/assembly/</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <!-- 配置描述符文件 -->
                    <appendAssemblyId>true</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- 将组装绑定到maven生命周期的哪一阶段 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 指定assembly插件的打包方式 -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <!-- 排除默认资源文件 -->
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>
  • assembly.xml
<assembly>
    <id>assembly</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>

    <!-- 文件设置,你想把哪些文件包含进去,或者把某些文件排除掉,都是在这里配置-->
    <fileSets>
        <!-- 把项目自己编译出来的可执行jar,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <!-- 对资源文件进行打包 -->
        <fileSet>
            <!-- ${project.build.outputDirectory} 为构建过程输出目录,缺省为 target/classes -->
            <directory>${project.build.outputDirectory}/config</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

おすすめ

転載: www.cnblogs.com/flypopo/p/12114839.html