IDEA使用maven

用maven创建第一个HelloWorld

1、下载apache-maven
进入官网,点击download,下载zip文件
在这里插入图片描述
创建一个文件apache-maven,将zip文件解压进去,然后创建一个LocalRepository文件用于放本地仓库(我自己的路径F:\apache-maven\)
在这里插入图片描述
2、打开idea,打开设置File->Settings
在这里插入图片描述
选择Build,Execution,Deployment->Build Tools->Maven
在这里插入图片描述
里面的一项Maven home directory右边的…可以选择apache-maven,自己的刚才下载的zip文件
在这里插入图片描述
在F:\apache-maven\apache-maven-3.6.3\conf下的settings.xml进行设置
(1)设置本地仓库位置

<localRepository>F:\apache-maven\LocalRepository</localRepository>

(2)设置镜像

	<mirror>
      <id>nexus-aliyun</id>
      <mirrorOf>central</mirrorOf>
      <name>Nexus aliyun</name>  <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

(3)设置profile

	<profile>
	  <id>jdk-1.8</id>
	  <activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
	  </activation>
      <properties>
        <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>

(使用idea默认自带的settings.xml也可以使用,同样这么设置,地址在idea下载目录下plugins->maven->lib,下面两个选择两个maven)
在这里插入图片描述
在这里插入图片描述
细节点1:work offline去掉打钩
在这里插入图片描述
细节2:importing下的Import maven projects automatically打钩,Automatically download的sources和documentation也打钩
在这里插入图片描述
3、创建测试
新建一个模块File->New->Module
在这里插入图片描述
选择maven,第一次打开需要选择jdk1.8,然后下一步
在这里插入图片描述
填写GroupId和ArtifactId
在这里插入图片描述
创建项目名Hello
在这里插入图片描述
在src->main->java下创建一个类Hello,写一个sayHello方法在这里插入图片描述
在test->java下创建一个test方法
在这里插入图片描述
发现idea自动添加了语句,右边的maven projects的plugins下全部报错了
在这里插入图片描述
点击Lifecycle下的install尝试安装,发现报错

[ERROR] Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6: 1 problem was encountered while building the effective model
[ERROR] [FATAL] Non-parseable POM F:\apache-maven\LocalRepository\org\apache\maven\plugins\maven-resources-plugin\2.6\maven-resources-plugin-2.6.pom: unexpected markup <!d (position: START_DOCUMENT seen \r\n<!d... @2:4)  @ line 2, column 4
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

点击Execute Maven Goal,输入以下命令,更新不完整的命令

-U idea:idea

在这里插入图片描述
尝试Lifecycle下面的各种命令,都发现报错了,而且Plugins下仍然报错
在这里插入图片描述
在网上找了很多错误,基本没有出现全错的情况~ _ ~
我自己的做法:在pom.xml下把包名都输入进去(注意版本)

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
        </dependency>

在这里插入图片描述
在modules下添加三个包
在这里插入图片描述
测试完成
在这里插入图片描述

idea打包jar包

打开maven,依次选择clean、compile和install
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/skybulex/article/details/115140476