maven command examples

本文详细介绍了Maven中常用的命令及其用途,包括构建应用、生成站点、运行单元测试等操作,并提供了创建项目的示例。

mvn install :  build application

 

mvn site    : generate application site

 

mvn test    : run unit test. (Put your test in src/test/java; add a test-scoped dependency on either TestNG or JUnit)

 

mvn -v       : check maven version, java version, os info

 

mvn help:describe -Dplugin=help          :  brief description on the plugin help

 

mvn help:describe -Dplugin=help -Dfull :  full description on the plugin help

 

mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull : full description on the plugin compiler, goal compile

 

mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook : create new simple empty project named simple

 

mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch04 -DartifactId=simple-weather

-DpackageName=org.sonatype.mavenbook -Dversion=1.0

 

mvn help:effective-pom    : final POM which is composed of current project's POM and all ancestor's POMs. 

 

mvn dependency:resolve    : list out the jar files needed by the project

 

mvn dependency:tree         : list out the entire dependency tree

 

 

 

 

 

 

mvn one:convert       : convert maven 1 to maven 2 projects.

 

<think>我们正在处理一个关于在IDEA Maven项目中打包时出现的CreateProcess错误206(文件名或扩展名太长)的问题。 根据用户提供的引用信息,特别是引用[2],我们可以看到一个解决方案:使用一个Gradle插件来缩短类路径字符串长度。 但是,用户使用的是Maven项目,而不是Gradle。因此,我们需要寻找适用于Maven项目的解决方案。 错误206通常是由于Windows系统对命令行长度有限制(大约32768个字符)导致的。当类路径(classpath)太长时,就会触发此错误。 针对Maven项目,我们可以考虑以下几种解决方案: 1. **使用Manifest文件**:通过将类路径信息放在manifest文件中,而不是在命令行中传递,可以缩短命令行长度。 在Maven中,我们可以使用`maven-jar-plugin`或`maven-assembly-plugin`来生成manifest文件,并在其中指定类路径。 2. **使用类路径通配符(Wildcards)**:从Java 6开始,可以使用通配符来指定类路径。例如,使用`java -cp libs/*`来加载libs目录下的所有jar包。 在Maven中,我们可以配置`maven-jar-plugin`或`maven-surefire-plugin`(用于测试)来使用通配符。 3. **使用`maven-shade-plugin`**:将多个jar包合并成一个uber-jar,这样可以减少类路径中的jar数量。 4. **使用`spring-boot-maven-plugin`(如果是Spring Boot项目)**:它默认会生成一个可执行的jar,其中包含了所有依赖。 5. **缩短项目路径**:将项目放在更靠近根目录的路径下,以减少路径长度。 6. **使用`MANIFEST.MF`文件并设置`Class-Path`**:在MANIFEST.MF中指定类路径,这样命令行中只需要指定一个jar文件,然后通过manifest文件中的类路径来加载其他jar。 7. **修改Windows注册表**:增加Windows命令行的长度限制(不推荐,因为涉及系统修改,且可能不安全)。 由于用户使用的是IDEA,我们还可以尝试在IDEA中设置: - 在IDEA的运行/调试配置中,缩短命令行参数或使用缩短类路径的选项(例如,使用@argfile方式,即Java 9引入的类路径文件方式)。 但是,对于Maven打包(package)阶段,我们主要关注Maven的配置。 下面,我们重点介绍两种在Maven中常用的解决方案: **解决方案1:使用`maven-jar-plugin`配置manifest文件** 在pom.xml中配置: ```xml <build> <plugins> <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> </manifest> </archive> </configuration> </plugin> </plugins> </build> ``` 然后,使用`maven-dependency-plugin`将依赖复制到`target/lib`目录下。这样,生成的jar的manifest中会包含类路径,类路径是相对路径,指向lib目录下的所有jar。 但是,这种方式在运行程序时,需要将jar和lib目录一起发布,并且使用`java -jar your.jar`来运行。 **解决方案2:使用`maven-assembly-plugin`创建包含依赖的jar** 使用`maven-assembly-plugin`创建一个包含所有依赖的jar(uber-jar): ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>your.main.Class</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ``` 这样会生成一个包含所有依赖的jar,运行时就只有一个jar文件,因此不会出现类路径过长的问题。 **解决方案3:使用`maven-shade-plugin`** 与`maven-assembly-plugin`类似,但提供了更多高级功能,比如重命名类等: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>your.main.Class</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> ``` **解决方案4:使用Java 9+的类路径文件(@argfile)** 如果你使用的是Java 9及以上版本,并且Maven相关插件支持,可以尝试使用类路径文件。在Maven的插件配置中,我们可以尝试启用这个特性。但是,这通常是在运行程序时使用的,对于打包过程,我们可能需要配置Maven的插件(如surefire)使用类路径文件。 例如,对于`maven-surefire-plugin`(测试阶段): ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <argLine>@{argLine}</argLine> </configuration> </plugin> ``` 这会将类路径写入一个文件,然后在命令行中通过@filename引用,从而缩短命令行长度。 但是,对于打包阶段,我们可能没有直接使用这个机制。因此,对于打包错误,更推荐使用前面提到的生成uber-jar的方式。 总结:对于Maven项目,解决CreateProcess error=206的最常用方法是生成一个包含所有依赖的uber-jar(使用`maven-assembly-plugin`或`maven-shade-plugin`),或者使用manifest文件来缩短类路径字符串。 请用户根据项目情况选择合适的方法。如果项目需要生成可执行的jar,推荐使用uber-jar的方式。 如果用户不想改变打包方式,也可以尝试在IDEA中设置缩短命令行参数: 1. 打开IDEA的设置(Settings),找到Build, Execution, Deployment -> Compiler。 2. 在Shared build process VM options中,设置:`-Djdk.util.ProxyGenerator.saveGeneratedFiles=true`(这个不一定有用) 3. 或者,尝试在运行配置中,选择“Shorten command line”选项,并选择“JAR manifest”或“classpath file”。 但是,这个设置可能只影响运行和调试,不影响Maven打包。因此,我们主要还是通过配置Maven插件来解决。 根据引用[2]中提到的Gradle插件,我们也可以寻找Maven中类似的插件。有一个Maven插件`maven-classpath-plugin`(但似乎不常用),或者我们可以通过配置`maven-jar-plugin`来生成manifest类路径。 因此,我们提供两种主要方案: 方案A(推荐):使用`maven-shade-plugin`创建包含依赖的jar。 方案B:使用`maven-jar-plugin`配合`maven-dependency-plugin`,将依赖复制到lib目录,并在manifest中设置类路径。 下面给出方案A的详细配置: 在pom.xml中添加: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.yourcompany.MainClass</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 请将`com.yourcompany.MainClass`替换为你的主类全限定名。 方案B的详细配置: 步骤1:使用`maven-dependency-plugin`复制依赖到`target/lib`: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.2</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> ``` 步骤2:配置`maven-jar-plugin`生成manifest: ```xml <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>com.yourcompany.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> ``` 这样,打包后,在`target`目录下会有生成的jar和lib目录(包含所有依赖jar)。运行程序时,需要将整个target目录(或jar和lib目录一起)发布,并使用`java -jar your.jar`运行。 用户可以根据需要选择方案。 最后,如果用户的项目是一个Web应用(war包),则通常不会出现这个问题,因为war包有固定的结构。因此,我们假设用户是在构建可执行的jar包。 请用户尝试上述解决方案,并反馈结果。 相关问题: 1. 如何配置Maven生成可执行的jar包? 2. Maven中`maven-shade-plugin`和`maven-assembly-plugin`有什么区别? 3. 在IDEA中如何设置缩短命令行参数? 4. 除了修改Maven配置,还有哪些方法可以解决Windows下命令行过长的问题?</think>### 解决IDEA Maven打包错误:CreateProcess error=206(文件名过长) 该错误是由于Windows系统对命令行长度有限制(约32767字符),当Maven构建的类路径过长时触发。以下是针对IDEA的解决方案: #### 方案1:使用Manifest文件缩短类路径(推荐) 在`pom.xml`中添加`maven-jar-plugin`配置: ```xml <build> <plugins> <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> <!-- 依赖将放入lib目录 --> <mainClass>com.your.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ``` 1. 执行`mvn clean package`生成JAR 2. 手动将依赖复制到`target/lib`目录 3. 通过`java -jar your-app.jar`运行 #### 方案2:创建Uber-JAR(包含所有依赖) 使用`maven-shade-plugin`合并所有依赖: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.your.MainClass</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> ``` #### 方案3:IDEA内置解决方案 1. 打开 **Run/Debug Configurations** 2. 选择你的Maven目标 3. 在 **Command line** 添加: ``` -Dmaven.ext.class.path="C:\path\to\ManifestClasspath.jar" -DManifestClasspath.manifest.jarpath=target/lib ``` (需先下载[ManifestClasspath插件](https://github.com/eshepelyuk/manifest-classpath-plugin)) #### 方案4:系统级优化 1. **缩短项目路径**:将项目移至根目录(如`C:\project`) 2. **启用长路径支持**(需Windows 10+): - 组策略编辑器 → 计算机配置 → 管理模板 → 系统 → 文件系统 → 启用`win32长路径` 3. **减少依赖数量**: ```bash mvn dependency:tree -Dverbose | grep conflict # 检查冲突依赖 mvn dependency:analyze # 分析无用依赖 ``` #### 验证方案 执行打包命令前添加参数查看类路径长度: ```bash mvn package -X | findstr /i /c:"Classpath" ``` > **注意**:如果使用Spring Boot,优先使用`spring-boot-maven-plugin`,它天然支持长类路径处理[^1]。 --- ### 相关问题 1. **如何检查Maven依赖树中的冲突?** 2. **Spring Boot项目如何处理长类路径问题?** 3. **除了文件名过长,还有哪些常见Maven打包错误?** 4. **如何优化Maven构建速度?** [^1]: [Apache Maven官方文档](https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html) [^2]: [ManifestClasspath插件文档](https://github.com/eshepelyuk/manifest-classpath-plugin)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值