외부에서 구성 파일을 수정할 수 있습니다 메이븐 패키저를 사용하는 방법

육개월 과거의 상황이 매우 복잡하고 오랜 시간이 더 이상 보에 도달합니다. 이 두 가지 일 자바의 개발, 매우 실망스러운 경험 문제, 단지 미래의 참조를 위해 기록을 탐구하기 시작. 문제는 데이터베이스에 XML 파일 정보에 발생, XML 파일이 아니라 특별한 데이터베이스 해당 라벨의 필드 이름이 동일하지 않습니다, 별도의 설정 파일을 관리 할 필요가, 내가 JSON 구성 파일로 구성됩니다 에 직접 후자를 수정합니다. 최근 자바 관련 콘텐츠에서 그냥 보면, 나는 당신의 손을 연습하기 위해 자바를 사용하고 싶었다. 기능은 곧 achieve'd 하루 종일 어떻게 동적 프로필 카드의 결과를 얻을 수 있습니다.

문제 설명

리소스 파일 (구성 파일) 메이븐이 패키지를 받는다는 - 조립 플러그인 패키지 병에 포장되지 용도는, 수동으로 수정 될 수있다 방법

해결 과정

단차 구덩이 1 : SQLSERVER 드라이버 문제

다음과 같이 SQLSERVER 메이븐 저장소 사이트는 드라이브에 직접 의존 주어진 :

<!-- 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