在本文中,我们将带你了解Eclipse中使用Maven创建Web时错误在这篇文章中,我们将为您详细介绍Eclipse中使用Maven创建Web时错误的方方面面,并解答eclipse创建mavenweb
在本文中,我们将带你了解Eclipse 中使用 Maven 创建 Web 时错误在这篇文章中,我们将为您详细介绍Eclipse 中使用 Maven 创建 Web 时错误的方方面面,并解答eclipse创建maven web工程常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的
- Eclipse 中使用 Maven 创建 Web 时错误(eclipse创建maven web工程)
maven 项目配置 maven 私服,从私服下载 jar - eclipse maven build、maven clean、maven install和maven test的区别 精析
- Eclipse 中使用 Maven 创建 Servlet3.0 Web 项目
- Eclipse 使用 Maven 创建 Web 时错误:Could not resolve archetype org.apache.maven.archetypes:maven-ar
Eclipse 中使用 Maven 创建 Web 时错误(eclipse创建maven web工程)
一、问题描述
使用 Eclipse 创建 Maven 项目时,报一下错误,不能创建成功。
二、问题原因
错误详细描述是说
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any of the configured repositories.
大致是说无法从任何已经配置的库里面解析 org.apache.maven.archetypes:maven-archetype-webapp:1.0。
造成原因:
a. 网络无法下载 Maven 的包。
b. 下载 jar 包到本地仓库时会产生.lastupdate 文件而导致该包无法使用,而是安装默认的路径 “C:\Users\xxxx\.m2\repository\org\apache\maven\archetypes”。
三、解决方案
a. 删除安装路径下的 maven 文件夹。
b. 在 Eclipse Maven 配置中添加新的 Remote Catalog 配置。
c. 创建项目时选择新的 Remote Catalog 配置。
d. 创建成功后的项目选中点右键找到属性,在 build path 中可以看到有几个文件不存在,可以按照缺失的增加上。
e. 创建完后,JSP 页面出现错误
The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path。
右键点击项目 ->build path->configure build path->add library->server runtime->apache tomcat
maven 项目配置 maven 私服,从私服下载 jar
首先,保证 public 池里有 自定义 respository
然后,保证总的 settings.xml 的 mirrors 中,public 在最上面.
两种方式
1.pom 文件配置私服地址
<!-- 仓库地址 -->
<repositories>
<repository>
<id>public</id>
<name>Public Repositories</name>
<url>http://192.168.3.17/nexus/content/groups/public/</url>
</repository>
</repositories>
<!-- 插件地址 -->
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://192.168.3.17/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
在构建 maven 项目时,如果在私服中存在需要的构件,则会直接从私服中下载;
如果私服中没有所需构件,则会先从网络上下载到私服,之后才会下载到本地。
2. 全局配置 maven 私服
修改全局的 settings.xml 文件
<profiles>
<profile>
<id>public</id>
<repositories>
<repository>
<id>Public Repositories</id>
<url>http://192.168.3.17/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>Public Repositories</id>
<url>http://192.168.3.17/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>public</activeProfile>
</activeProfiles>
eclipse maven build、maven clean、maven install和maven test的区别 精析
1.情景展示
选中maven项目,右键-->Run As或Debug As-->maven buid,maven install,maven test有什么区别?
2.区别说明
6 Maven clean-->执行的是maven的原生命令:
mvn clean
表示:删除target目录。
原目录结构存在target目录
执行该命令后, target目录被删除。
2018/11/14
选中target目录-->右键-->刷新-->在windows资源管理器下打开该目录,你会发现class已经重新编译好
注意:这不是mvn clean命令执行的只是清空target目录,classes文件之所以重新被编译,依赖于eclipse的自动编译功能!
7 Maven generate-sources-->执行的是maven的原生命令:
mvn generate-sources
表示:会根据pom配置去生成源代码格式的包,产生应用需要的任何额外的源代码(没用过)
8 Maven install-->执行的是maven的原生命令:
mvn install
表示:对项目进行编译、将项目打成war包并且发布到本地仓库。
target目录说明:
classes目录:.class,.properties,.xml文件;
项目名目录:项目所有源码(前端、后台、jar包)
项目名.war:将项目名目录打成的war包。
生成的target目录展示
安装到本地仓库的目录展示
9 Maven test-->执行的是maven的原生命令:
mvn test
表示的是:对项目进行编译并执行测试代码。
生成的目录:
5 Maven build...-->maven没有mvn buid命令
表示的是:这是eclipse自带的mvn插件,通过这个功能,你可以自定义设置要执行的maven命令。
用这个功能,我们能干什么?
举2个栗子:
案例一:
因为maven在打包时,如果已经存在一个包时,再次执行打包命令时,如果不把原来的war包删除,经常会导致包没有更新;
这时,我们就需要先清除该war包(执行clean命令对应eclipse中的Maven clean功能),再重新打包(执行package或install命令对应eclipse中的Maven install功能)。
我们可以使用"Maven build"这个功能,在"Goals"一栏,输入命令:clean install,执行一次即可。
案例二:
对项目进行打包的时候,跳过单元测试。
eclipse没有这个功能,只能通过自定义maven命令来实现,在Maven build...这个功能进行设置。
示例一:打包并发布到本地仓库
错误用法:不设置命令,直接run
相当于maven命令:
mvn
报错信息如下:
正确用法:输入 install
示例二:打包时,跳过单元测试
方式一:在上图勾选上“Skip Tests”
使用这种方式:src/test/java和src/test/resources这两个目录下的文件不会被编译,自然不会被添加到编译好的项目当中。
方式二:声明maven命令:“-DskipTests”
使用这种方式: src/test/java和src/test/resources这两个目录下的文件依然会被编译,但不会被添加到编译好的项目当中。
4 Maven build
表示的是:执行已经定义好的maven命令。
一个项目可以“Maven build...”功能定义n个maven命令,然后通过“Maven build”功能来执行想要执行的命令。
Run Configurations...
可通过这个功能,对已经自定义的maven命令进行新建、修改、运行、删除;
如何删除多余的maven命令?
选中不需要的maven命令,右键-->“删除”即可。
3.maven其它命令说明
mvn compile
表示的是:对项目源代码进行编译。
生成的目录:
mvn deploy
表示的是:将项目发布到远程仓库。
mvn dependency:tree
表示的是:显示maven依赖树。
mvn dependency:list
表示的是:显示maven依赖列表。
mvn package
表示的是:编译项目并打成war包。
跳过单元测试的两种方式:
方式一:
-DskipTests
方式二:
-Dmaven.test.skip=true
表示的是:测试类不会被编译,或即使被编译也不会被添加到项目当中。
另外,这个指令没有先后顺序,均能正常执行。
举例: 以打包并发布到本地仓库时,跳过测试类为例。
// 方式一:形式一
mvn install -DskipTests
// 方式一:形式二
mvn -DskipTests install
// 方式二:形式一
mvn install -Dmaven.test.skip=true
// 方式二:形式二
mvn -Dmaven.test.skip=true install
写在最后
哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!
相关推荐:
- maven 配置环境变量
Eclipse 中使用 Maven 创建 Servlet3.0 Web 项目
Apache Maven 是一个优秀的项目构建和管理工具,许多开源项目都使用 Maven 进行构建。由于最近工作中要用到 Maven,于是这里记录下在 Eclipse 中使用 Maven 插件创建一个基于 Servlet3.0 的 Java Web 项目的过程,欢迎大家指正。
-
安装 Maven:
-
下载 Maven:在 Apache 官方网站 http://maven.apache.org/download.cgi 下载 Maven3.1.1,解压后放在 E:/maven 目录下 E:\maven\apache-maven-3.1.1;
-
配置环境变量:在我的电脑 > 属性 > 高级 > 环境变量 > 系统变量 > 新建:变量名:M2_HOME ,变量值:F:\maven\apache-maven-3.0.3,然后将在 path 环境变量值尾部加入:;%M2_HOME%\bin;
-
检查配置:运行 > cmd,在命令行中输入: mvn -v 命令检查配置,Maven 在运行时需要使用到 JDK,所以安装 JDK 是前提条件了。如果没有提示错误信息,maven 安装完成;
-
修改仓库位置:Maven 默认的仓库位置当前用户目录的 .m2/repository 目录,为了便于管理,将仓库位置配置到 E:/maven 下的 /repository 目录,用文本编辑器打开 E:\maven\apache-maven-3.1.1\conf 下的 settings.xml ,在 <settings> 节点下添加本地仓库的位置 <localRepository>E:/maven/repository</localRepository> ,如图下图;
到这里,Maven 就安装好了,可以在命令行下使用 Maven 了,下面要做的就是把 Maven 集成到 Eclipse 中。
-
Eclipse 安装 m2e 插件:
-
安装 m2e 插件:Eclipse Kepler 以后版本都集成了 m2e 插件,如果没有集成可以到 m2e 官网下载安装:http://www.eclipse.org/m2e/;
-
配置 m2e 插件:安装完插件后,在 Window>Preferences>Maven>Installations 中去掉 m2e 内置的 maven,点击右边 Add 按钮,选择上边安装好的 apache-maven-3.1.1 , 然后勾选,apply,如图;
-
配置用户设置:将 E:\maven\apache-maven-3.1.1\conf 下的 settings.xml 文件复制到 E:/maven 目录下,改名为 user_settings.xml ,在 Window>Preferences>Maven>User Settings 中选择该文件,当然那个文件只配置了仓库位置,还可以配置其他参数,这个配置文件是用户配置文件,而 /conf 下的 settings.xml 则是 Maven 的全局配置文件;
到这里,就将 Maven 集成进了 Eclipse 中,就可以使用 Eclipse 的可视化界面使用 Maven 了。
-
创建支持 Java Servlet3.0 的 Maven Webapp 项目:
-
创建 Maven webapp:File>New>Maven Project>Next Archetype 选择 maven-archetype-webapp 下一步,填写项目信息,finish,如图;
-
完善项目 src 目录结构:由于标准的 Maven webapp 项目 src/(main(java、resources、webapp),(test(java、resources))),而创建的项目 main 下缺少 java 目录,也没有 test 目录,这里分别创建;
-
修改 webapp 的版本:默认创建的 webapp 的版本 2.3,这里需要修改成支持 Servlet3.0 的 webapp3.0。在 workspace 中找到 HelloMaven 项目,编辑 /HelloMaven/.settings/org.eclipse.wst.common.project.facet.core.xml, 将 <installed facet="java" version="1.5"/> ,<installed facet="jst.web" version="2.3"/> 改为 <installed facet="java" version="1.6"/> ,<installed facet="jst.web" version="3.0"/>。再 Eclipse 打开项目的 pom.xml 文件,在 <build> 节点下添加如下配置:
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins>
右键项目 Maven>Update Project 更新项目,然后 Properties>Project Facets 中,将 Dynamic Web Module 改为 3.0、Java 改为 1.6、右边 Runtimes 选择 Tomcat7.0(Servlet3.0 需要 Tomcat7.0+),如图。
最后删掉 src/main/webapp/WEB-INF/web.xml,再次 Maven>Update Project 更新项目。
4. 创建 Servlet 测试:创建一个基于 Servlet3.0 的 HelloMavenWebappServlet,代码如下,将项目部署到 Tomcat7 启动并访问 http://localhost:8080/HelloMaven/helloMavenWebapp ,页面输出 Get:/helloMavenWebapp。
@WebServlet("/helloMavenWebapp")
public class HelloMavenWebappServlet extends HttpServlet{
private static final long serialVersionUID = 8963265462953694987L;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println("Get:/helloMavenWebapp");
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println("Post:/helloMavenWebapp");
}
}
终于将使用 Maven 构建的 Java Web 项目创建成功了,更多关于 Maven 的配置和使用正在学习中,欢迎大家指正!
原文:成都四维时空
Eclipse 使用 Maven 创建 Web 时错误:Could not resolve archetype org.apache.maven.archetypes:maven-ar
Eclipse 使用 Maven 创建 Web 时错误:Could not resolve archetype org.apache.maven.archetypes:maven-ar
喜欢本站的朋友可以收藏本站,或者加入 QQ 群:172816590, 我们大家一起来交流技术!
欢迎来到梁钟霖个人博客网站。本个人博客网站提供最新的站长新闻,各种互联网资讯。 还提供个人博客模板,最新最全的 java 教程,java 面试题。在此我将尽我最大所能将此个人博客网站做的最好!谢谢大家,愿大家一起进步!
使用 Eclipse 自带的 Maven 插件创建 Web 项目时报错:
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.
Could not resolve artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE
Failed to resolve version for org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype-webapp/maven-metadata.xml in local (C:\Users\liujunguang\.m2\repository)
Failed to resolve version for org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype-webapp/maven-metadata.xml in local (C:\Users\liujunguang\.m2\repository)
解决方案:
1. 在 Eclipse Maven 配置中添加新的 Catalog 配置:
- http://repo1.maven.org/maven2/archetype-catalog.xml
如图:
接下来在使用刚添加的 catalog 创建 web 工程
这个时候就可以看到 Eclipse 联网下载了:
如果还是不能创建或者问题没有解决可以安装下面的方法:
1. 删除 maven 已经下载的文件路径:.m2\repository\org\apache\maven
删除之后再试下,这时候如果看到有进度,只需要耐心等待就行了
2.Maven 下载 jar 慢可以使用国内阿里镜像
具体配置文件(settings.xml)如下,放在.m2 文件夹下,.m2 一般位于用户目录下
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- 这个是配置阿里 Maven 镜像 --> <mirrors> <mirror> <id>aliyun</id> <name>aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://repo.maven.apache.org/maven2</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>ansj-repo</id> <name>ansj Repository</name> <url>http://maven.nlpcn.org/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings> |
如果此时还不成功,可以查看下当前 maven 创建 webapp 的 jar 包是否存在,找到自己的 response,我的目录是在
C:\Users\windowsusr\.m2\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.3 下
文件夹里发现下载 jar 包失败,解决方案如下:
去 maven 仓库下载:https://mvnrepository.com/search?q=maven-archetype-webapp-1.3,如下图
点击 view 进去后下载必要的文件到本地私有仓库里
然后重新创建一遍 maven springmvc 项目后,成功!!
转载至链接:https://my.oschina.net/u/193508/blog/2872838。
今天关于Eclipse 中使用 Maven 创建 Web 时错误和eclipse创建maven web工程的介绍到此结束,谢谢您的阅读,有关
本文标签: