Maven 指定 JDK 版本、仓库源、要打包的文件

一、JDK 版本和仓库源

全局指定 settings.xml

复制代码
<localRepository>D:\repository\</localRepository>

<mirrors>
  <mirror>
    <id>central</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyunmaven</name>
    <url>https://maven.aliyun.com/repository/central</url>
  </mirror>
  <mirror>
    <id>apache.snapshots.https</id>
    <mirrorOf>apache.snapshots.https</mirrorOf>
    <name>aliyunmaven</name>
    <url>https://maven.aliyun.com/repository/apache-snapshots</url>
  </mirror>
</mirrors>

<profiles>
  <profile>
    <id>jdk-1.8</id>
    <activation>
      <jdk>1.8</jdk>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
  </profile>
</profiles>
View Code
复制代码

当前项目指定 pom.xml

复制代码
<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
    <repository>
        <id>public</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>public</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

<build>
    <plugins>
        <plugin><!-- 指定 JDK 版本 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <skip>true</skip>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <compilerVersion>${maven.compiler.target}</compilerVersion>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <plugin><!-- 跳过测试 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
            <configuration>
                <skip>true</skip>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>
View Code
复制代码

 

二、要打包的文件

复制代码
<build>
    <resources>
        <resource>
            <!--控制资源目录下要打包进去的文件,这里为全部打包-->
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <!--细粒度控制-->
            <directory>src/main/resources2</directory>
            <includes>
                <include>**/*.txt</include>
            </includes>
            <excludes>
                <exclude>**/*.jpg</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>UTF-8</encoding>
                <useDefaultDelimiters>true</useDefaultDelimiters>
                <resources>
                    <!--指定资源目录-->
                    <resource>src/main/resources</resource>
                    <resource>src/main/resources2</resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>
View Code
复制代码

打包排除 jar 文件

复制代码
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/*.jar</exclude>
            </excludes>
        </resource>
    </resources>
</build>
View Code
复制代码

打包时对某些文件不进行编码

项目中有 excel 等类型文件时,对这些文件进行编码会出现问题,如损坏等

最好是不要把这些文件打包进 jar 或 war 中,要打包就不要对其进行编码

复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <nonFilteredFileExtensions>
                    <!--过滤掉不需要编码的文件:过滤后缀为 .doc、.xlsx。。。 的所有文件,不对其进行统一编码-->
                    <nonFilteredFileExtension>doc</nonFilteredFileExtension>
                    <nonFilteredFileExtension>vsd</nonFilteredFileExtension>
                    <nonFilteredFileExtension>txt</nonFilteredFileExtension>
                    <nonFilteredFileExtension>ftl</nonFilteredFileExtension>
                    <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                    <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>
    </plugins>
</build>
View Code
复制代码

 

三、清除临时文件

有时本地依赖存在却提示找不到,这时删除仓库中 _remote.repositories 和 .lastUpdated 即可

# Windows,在仓库目录打开 cmd
for /r %i in (*.lastUpdated) do del %i
for /r %i in (*.repositories) do del %i
# Linux
find /maven/repository -name "*.lastUpdated" -exec grep -q "Could not transfer" {} \; -print -exec rm {} \;
find /maven/repository -name "*.repositories" -exec grep -q "Could not transfer" {} \; -print -exec rm {} \;

或直接指定本地依赖路径

<repositories>
    <repository>
    <id>local</id>
        <url>file:D:\repo</url>
    </repository>
</repositories>

 


http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html

http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

https://search.maven.org & https://central.sonatype.com

https://oss.sonatype.org & https://repository.sonatype.org

https://developer.aliyun.com/mirror/maven & https://mirrors.cloud.tencent.com/help/maven.html & https://mirrors.huaweicloud.com/mirrorDetail/5ea0025f2ab89b484a4dd5ce

https://github.com/comwrg/package-manager-proxy-settings & https://github.com/eryajf/Thanks-Mirror

posted @   江湖小小白  阅读(23104)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示