【Maven】将普通Eclipse项目改造为Maven项目(非SpringBoot项目)
- IT业界
- 2025-09-17 18:33:01

文章目录 将普通Eclipse项目改造为Maven项目(非SpringBoot项目)Maven安装与配置项目结构改造父子Pom.xml文件配置(继承与集成)父项目下的pom.xml文件配置普通子模块下的pom.xml配置启动模块的pom.xml配置 多模块编译总结 Maven插件配置一、maven-compiler-plugin功能常用参数配置示例注意事项 二、maven-jar-plugin功能常用参数配置示例注意事项 三、maven-dependency-plugin功能常用参数配置示例注意事项 四、maven-resources-plugin功能常用参数配置示例注意事项 五、maven-shade-plugin功能常用参数配置示例注意事项 六、maven-assembly-plugin功能常用参数配置示例自定义`assembly.xml`示例注意事项 总结 将普通Eclipse项目改造为Maven项目(非SpringBoot项目) Maven安装与配置
网上资料很多,可自行搜索,不再赘述。
项目结构改造需要将普通Eclipse项目结构改造为Maven标准结构,如下
src/ |--main/ |--java/ |--resources/多模块项目结构
Demo/ |--module1/ |--java/ |--resources/ |--pom.xml |--module2/ |--java/ |--resources/ |--pom.xml |--module3/ |--java/ |--resources/ |--pom.xml |--pom.xml 父子Pom.xml文件配置(继承与集成) 父项目下的pom.xml文件配置注意事项:
为了方便管理子模块的依赖,防止出现依赖冲突,在这里配置好所有模块的依赖项,后面等所有模块配置好后会一起下载依赖项按子模块间的依赖顺序集成子模块,作为后续构建jar包的打包顺序可配置下载依赖的Maven仓库地址 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// .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> <!-- GAV坐标 --> <groupId>com.regan</groupId> <!-- 公司或组织ID --> <artifactId>Demo</artifactId> <!-- 项目模块ID --> <version>1.0.0</version> <!-- 版本号 --> <packaging>pom</packaging> <!-- 打包形式,pom工程 --> <name>Demo</name> <!-- 项目名称 --> <description>Demo</description><!-- 项目描述 --> <!-- 自定义属性 --> <properties> <!-- 应用版本号 --> <demo.version>1.0.0</demo.version> <!-- 编译版本 --> <maven piler.source>8</maven piler.source> <maven piler.target>8</maven piler.target> <!-- 编码格式 --> <project.build.sourceEncoding>GBK</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Java版本 --> <java.version>1.8</java.version> <!-- 第三方库版本 --> <dom4j.version>2.1.3</dom4j.version> <fastjson2.version>2.0.53</fastjson2.version> <gson.version>2.8.9</gson.version> <log4j-core.version>1.2.17</log4j-core.version> <junit.version>5.9.2</junit.version> <!-- 外部资源路径 --> <external-resources-path>${project.basedir}/../..</external-resources-path> <!-- maven插件 --> <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> <maven-jar-plugin.version>3.3.0</maven-jar-plugin.version> <maven-shade-plugin.version>3.2.4</maven-shade-plugin.version> <maven-dependency-plugin.version>3.6.0</maven-dependency-plugin.version> <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version> </properties> <!-- 依赖项管理 --> <dependencyManagement> <dependencies> <!-- xml解析 --> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>${dom4j.version}</version> </dependency> <!-- 阿里json解析器 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>${fastjson2.version}</version> </dependency> <!-- json-java object 转换 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${gson.version}</version> </dependency> <!-- 日志记录工具 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j-core.version}</version> </dependency> <!-- 编写测试的API --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 基础模块 --> <dependency> <groupId>com.regan</groupId> <artifactId>module1</artifactId> <version>${demo.version}</version> </dependency> <!-- 基础工具模块 --> <dependency> <groupId>com.regan</groupId> <artifactId>module2</artifactId> <version>${demo.version}</version> </dependency> <!-- 核心模块 --> <dependency> <groupId>com.regan</groupId> <artifactId>module3</artifactId> <version>${demo.version}</version> </dependency> <!-- 启动模块 --> <dependency> <groupId>com.regan</groupId> <artifactId>demo</artifactId> <version>${demo.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- 子模块,根据依赖顺序排序 --> <modules> <module>module1</module> <module>module2</module> <module>module3</module> <module>demo</module> </modules> <!-- 统一管理插件版本 --> <build> <!-- 打包后的Jar包名称 --> <finalName>Demo-${project.artifactId}-${demo.version}</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </pluginManagement> </build> <!-- 仓库 --> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url> maven.aliyun /repository/public</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <!-- 插件仓库 --> <pluginRepositories> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url> maven.aliyun /repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project> 普通子模块下的pom.xml配置注意事项
注意要继承parent依赖的其它模块也要写上 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// .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"> <!-- 继承父级 --> <parent> <groupId>com.regan</groupId> <artifactId>Demo</artifactId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <!-- 子模块打包成Jar包 --> <artifactId>module2</artifactId> <description>基础模块</description> <dependencies> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> </dependency> <!-- 依赖的其它模块 --> <dependency> <groupId>com.regan</groupId> <artifactId>module1</artifactId> </dependency> </dependencies> </project> 启动模块的pom.xml配置注意事项
启动模块需要配置程序入口
在这里进行打包配置(依赖、资源文件)
如果所依赖的资源路径在项目执行路径之外(${project.basedir}),就需要在打包配置时使用
${project.basedir}/..–相当于执行路径下的上一级目录
<resources> <resource> <directory>你的资源路径</directory> </resource> </resources> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// .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"> <parent> <groupId>com.regan</groupId> <artifactId>Demo</artifactId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>module3</artifactId> <properties> <external-resources-path>${project.basedir}/../..</external-resources-path> </properties> <description>启动模块</description> <dependencies> <!-- xml解析 --> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>${dom4j.version}</version> <scope>system</scope> <systemPath>${external-resources-path}/safri_build/dom4j-2.1.1.jar</systemPath> </dependency> <!-- 日志记录工具 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j-core.version}</version> <scope>system</scope> <systemPath>${external-resources-path}/safri_build/log4j-1.2.17.jar</systemPath> </dependency> <!-- json库 --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>${json.version}</version> <scope>system</scope> <systemPath>${external-resources-path}/safri_build/json.jar</systemPath> </dependency> <!-- 语法高亮文本编辑器 --> <dependency> <groupId>com.fifesoft</groupId> <artifactId>rsyntaxtextarea</artifactId> <version>${rsyntaxtextarea.version}</version> <scope>system</scope> <systemPath>${external-resources-path}/safri_build/rsyntaxtextarea-3.5.3.jar</systemPath> </dependency> <!-- 阿里json解析器 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>${fastjson2.version}</version> <scope>system</scope> <systemPath>${external-resources-path}/safri_build/fastjson2-2.0.53.jar</systemPath> </dependency> <dependency> <groupId>com.regan</groupId> <artifactId>module1</artifactId> </dependency> <dependency> <groupId>com.regan</groupId> <artifactId>module2</artifactId> </dependency> </dependencies> <!-- 配置可执行Jar --> <build> <!-- 可选:配置从指定目录下开始编译,不拘泥于Maven结构-- <sourceDirectory>src</sourceDirectory> --> <plugins> <!-- 1. 生成主JAR(不含依赖) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>${maven-jar-plugin.version}</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>cnpo.Demo.app.ApplicationEntrance</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 2. 复制所有依赖到target/lib --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>${maven-dependency-plugin.version}</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <includeScope>runtime</includeScope> <includeScope>system</includeScope> </configuration> </execution> </executions> </plugin> <!-- 3. 处理资源文件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>${maven-resources-plugin.version}</version> <executions> <execution> <id>copy-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <!-- 资源1 目录 --> <resource> <directory>${external-resources-path}/images</directory> <excludes> <exclude>**/*.webg</exclude> </excludes> <targetPath>images</targetPath> </resource> <!-- config 目录 --> <resource> <directory>${external-resources-path}/config</directory> <includes> <include>**/*.*</include> </includes> <targetPath>config</targetPath> </resource> <!-- 单个文件 --> <resource> <directory>${external-resources-path}/</directory> <!-- 指向父目录 --> <includes> <include>*.properties</include> <include>admin.txt</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> 多模块编译进行多模块编译主要就是需要在父类pom.xml文件中配置好modules,各子模块的pom.xml文件中配置好依赖模块和依赖包,最后在启动模块下进行Maven插件的配置。可参考上述pom.xml配置文件。
还需要注意的一点是依赖配置:
若使用 <systemPath>,需在所有依赖模块中显式重复配置该依赖。 <scope>system</scope> <systemPath>依赖包路径</systemPath>注意:这种依赖作用域默认没有依赖传递。
总结Maven多模块项目主要就是注意父类Pom.xml文件与子模块Pom.xml文件的模块包含关系,以及依赖配置。
Maven插件配置以下是对Maven核心插件的详细介绍,涵盖参数配置、使用场景及注意事项:
一、maven-compiler-plugin 功能
负责Java源代码的编译,控制编译版本、编码、内存分配等。
常用参数 <source>:指定源代码兼容的Java版本(如1.8)。<target>:指定生成的字节码目标JVM版本。<encoding>:设置字符编码(默认UTF-8)。<fork>:是否启用独立进程编译(默认false)。<compilerArgs>:传递额外编译器参数(如-Xlint:unchecked)。<showWarnings>:是否显示编译警告(默认false)。<release>:兼容特定JDK版本(仅JDK9+支持)。 配置示例 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <compilerArgs> <arg>-Xlint:all</arg> </compilerArgs> </configuration> </plugin> 注意事项 版本兼容性:<release>参数仅JDK9+支持,低版本需用<source>/<target>。目标JVM限制:即使设置<target>为低版本,仍需避免使用高版本API,建议配合bootclasspath或Animal Sniffer插件验证。二、maven-jar-plugin 功能
生成标准JAR包,配置清单文件(MANIFEST.MF)。
常用参数 <archive>:配置JAR包元数据。 <manifest>:定义主类、类路径等。 <mainClass>:指定入口类。<addClasspath>:是否添加类路径(默认false)。<classpathPrefix>:类路径前缀(如lib/)。 <excludes>/<includes>:排除或包含特定文件。 配置示例 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.Main</mainClass> </manifest> </archive> <excludes> <exclude>**/test/**</exclude> </excludes> </configuration> </plugin> 注意事项 依赖管理:不会打包依赖库,需配合maven-dependency-plugin复制依赖到类路径目录。版本冲突:若配置不生效,检查插件版本与Maven/JDK兼容性。三、maven-dependency-plugin 功能
管理项目依赖,如复制依赖、生成依赖树。
常用参数 <copy-dependencies>目标: <outputDirectory>:依赖复制目标目录(如target/lib)。<excludeTransitive>:是否排除传递依赖(默认false)。<stripVersion>:移除依赖版本号(默认false)。 <dependency:tree>:生成依赖树分析。 配置示例 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <stripVersion>true</stripVersion> </configuration> </execution> </executions> </plugin> 注意事项 参数兼容性:避免使用旧版本参数(如overwrite),推荐使用<overWriteIfNewer>。依赖范围:通过<includeScope>控制复制范围(如runtime)。四、maven-resources-plugin 功能
处理资源文件,支持过滤、编码、动态替换占位符。
常用参数 <encoding>:资源文件编码(默认${project.build.sourceEncoding})。<resources>:定义资源目录及过滤规则。 <filtering>:是否启用占位符替换(默认false)。 <excludes>/<includes>:排除或包含特定资源文件。<escapeString>:转义占位符前缀(如\${}避免替换)。 配置示例 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.0</version> <configuration> <encoding>UTF-8</encoding> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>**/*.key</exclude> </excludes> </resource> </resources> </configuration> </plugin> 注意事项 二进制文件过滤:通过<nonFilteredFileExtensions>排除二进制文件(如PNG)。目录覆盖:默认包含src/main/resources,自定义资源目录需显式声明。五、maven-shade-plugin 功能
生成包含依赖的“胖JAR”(Uber-JAR),解决依赖冲突。
常用参数 <transformers>:合并资源文件(如ManifestResourceTransformer)。 <mainClass>:指定入口类。 <filters>:排除签名文件(如META-INF/*.SF)。<relocations>:重定位类路径以避免冲突(如com.google.guava→shaded .google.guava)。<createDependencyReducedPom>:是否生成精简POM(默认true)。 配置示例 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.Main</mainClass> </transformer> </transformers> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> 注意事项 资源冲突:多个依赖包含同名资源时需手动合并(如AppendingTransformer)。体积控制:通过<minimizeJar>移除未使用的类,但可能破坏反射。六、maven-assembly-plugin 功能
生成自定义分发包(如ZIP/TAR),包含脚本、配置文件等。
常用参数 <descriptorRefs>:使用预定义描述符(如jar-with-dependencies)。<descriptors>:引用自定义assembly.xml文件。<archive>:配置清单文件(如Main-Class)。<formats>:指定打包格式(如zip、tar.gz)。 配置示例 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <configuration> <descriptors> <descriptor>src/main/assembly/custom.xml</descriptor> </descriptors> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 自定义assembly.xml示例 <assembly> <id>full-distribution</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>target</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>src/main/scripts</directory> <outputDirectory>/bin</outputDirectory> </fileSet> </fileSets> </assembly> 注意事项 路径控制:<includeBaseDirectory>决定是否将文件放入顶级目录。依赖管理:通过<dependencySets>控制依赖的打包方式(如解压或直接复制)。总结 插件核心用途典型场景maven-compiler-plugin控制编译过程多版本JDK兼容、编码调整maven-jar-plugin生成标准JAR包可执行JAR、类路径配置maven-dependency-plugin依赖管理依赖复制、依赖树分析maven-resources-plugin资源文件处理配置文件过滤、编码统一maven-shade-plugin生成胖JAR微服务部署、依赖冲突解决maven-assembly-plugin定制化打包分发包含脚本/配置的完整发布包
通过合理配置这些插件,可实现从编译到分发的全流程控制,满足复杂项目需求。
【Maven】将普通Eclipse项目改造为Maven项目(非SpringBoot项目)由讯客互联IT业界栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【Maven】将普通Eclipse项目改造为Maven项目(非SpringBoot项目)”
上一篇
【数据挖掘】Matplotlib
下一篇
leetcode第39题组合总和