Maven 项目配置使用备忘录

  1. 创建 Maven 项目:
shell 复制代码
 mvn archetype:generate -DgroupId=com.ivandu -DartifactId=cmaimms -DarchetypeArtifactImaven-archetype-quickstart -DinteractiveMode=false

命令解释:

  • -DgroupId 组织Id(项目包名)。
  • -DartifactId ArtifactId(项目名称或者模块名称)。
  • -DarchetypeArtifactId 项目骨架。
  • -DinteractiveMode 是否使用交互模式。
  1. "File encoding has not been set, using platform encoding UTF-8" 问题解决:

IDE 字符编码设置为:UTF-8, IDEA 自定义环境加入:

properties 复制代码
-Dfile.encoding=utf-8

项目 pom.xml 加入:

xml 复制代码
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  1. "No goals have been specified for this build." 问题解决:

项目 pom.xml 加入:

xml 复制代码
<build>
    <defaultGoal>compile</defaultGoal>
</build>
  1. "[ERROR] 不再支持源选项 5。请使用 6 或更高版本。[ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。" 问题解决:
xml 复制代码
<properties>
    <java.version>11</java.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
  1. mvn spring-boot:run 报 "No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?" 错,解决如下:

在项目 pom.xml build 节点内加入:

xml 复制代码
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.1</version>
  <configuration>
    <verbose>true</verbose>
    <fork>true</fork>
    <executable>D:\Tools\Java\jdk-11.0.12.7-hotspot\bin\javac.exe</executable>
  </configuration>
</plugin>
  1. 一些常用配置:
xml 复制代码
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
</properties>
  1. 指定 JDK 版本、字符集配置:
xml 复制代码
<profile>
    <id>jdk-11</id>
    <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>11</jdk>
    </activation>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
    </properties>
</profile>
  1. Maven 阿里云公共仓库:
xml 复制代码
<mirror>
    <id>aliyunmaven</id>
    <mirrorOf>*</mirrorOf>
    <name>阿里云公共仓库</name>
    <url>https://maven.aliyun.com/repository/public</url>
</mirror>
  1. 无法读取配置文件,可以加入如下内容:
xml 复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
                <include>**/*.properties</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
  1. Maven scope 元素用于指定依赖库起作用的范围。该元素可指定如下值:

➢ compile:默认的范围,编译、测试、打包时需要。

➢ provided:表示容器会在运行时提供。

➢ runtime:表示编译时不需要,但测试和运行时需要,最终打包时会包含进来。

➢ test:只用于测试阶段。

➢ system:与provided类似,但要求该JAR是系统自带的。

11、Java执行Jar包报"jar中没有主清单属性"的解决方法:

xml 复制代码
<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>com.cxwn</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <url>https://maven.apache.org</url>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <attach>false</attach>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
相关推荐
Otaku love travel16 分钟前
老系统改造增加初始化,自动化数据源配置(tomcat+jsp+springmvc)
java·tomcat·初始化·动态数据源
DKPT29 分钟前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
learn_coder34 分钟前
在vscode中和obsidian中使用Mermaid
ide·vscode·编辑器
L_autinue_Star1 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
晨岳1 小时前
CentOS 安装 JDK+ NGINX+ Tomcat + Redis + MySQL搭建项目环境
java·redis·mysql·nginx·centos·tomcat
执笔诉情殇〆1 小时前
前后端分离(java) 和 Nginx在服务器上的完整部署方案(redis、minio)
java·服务器·redis·nginx·minio
YuTaoShao1 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
程序员的世界你不懂2 小时前
(20)Java+Playwright自动化测试- 操作鼠标拖拽 - 上篇
java·python·计算机外设
AI360labs_atyun2 小时前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai
不像程序员的程序媛3 小时前
redis的一些疑问
java·redis·mybatis