Maven依赖的是本地工程还是仓库安装jar包到本地仓库

让天下没有难学的技术
Maven安装第三方Jar包到本地仓库
Maven安装第三方Jar包到本地仓库
译者:logan
尽管很少存在这样的情况,有时在你的构建过程中,需要将第三方jar包添加到本地仓库中,因为它并存在于像Maven中央仓库或其它的公共仓库中。为了让Maven能够正确获取到jar包,第三方jar包必须放置到本地仓库的正确位置上。Maven已经提供了maven-install-plugin这样的插件能够使Jar包的安装过程更加容易、更少出错。可以用下面的命令安装一个JAR包到本地仓库。
mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=
如果也有pom文件的话,你可以使用下面的命令安装:
mvn install:install-file -Dfile= -DpomFile=
maven-install-plugin的2.5版本使安装过程更加好用,如果jar包是通过Maven构建的,它会在目录META-INF的子文件夹下创建一个pom.xml文件,这个Jar包会被默认读取。在这种情况下,你只需要这么做:
mvn install:install-file -Dfile=
原创文章,转载请注明: 转载自本文链接地址:
Latest posts by logan ()
Related posts:
(1 votes, average: 5.00 out of 5)
Loading...博客分类:
先说结果吧,要添加其它的远程仓库,需要在maven的conf目录下的setting.xml里面添加下面配置:
&profiles& 节点下添加(里面的url地址就是仓库的地址,根据自己的情况替换就好了):
&id&dev&/id&
&repositories&
&repository&
&id&company&/id&
&name&company&/name&
&url&http://192.168.2.202:8081/nexus/content/repositories/releases/&/url&
&releases&
&enabled&true&/enabled&
&/releases&
&snapshots&
&enabled&false&/enabled&
&/snapshots&
&/repository&
&/repositories&
&pluginRepositories&
&pluginRepository&
&id&company&/id&
&name&company&/name&
&url&http://192.168.2.202:8081/nexus/content/repositories/releases/&/url&
&releases&
&enabled&true&/enabled&
&/releases&
&snapshots&
&enabled&false&/enabled&
&/snapshots&
&/pluginRepository&
&/pluginRepositories&
&/profile&
同时在setting.xml最后&/settings&之前加上下面的(这里的dev就是上面repository的id):
&activeProfiles&
&activeProfile&dev&/activeProfile&
&/activeProfiles&
这样就可以从其它的仓库下载了
从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,输入:
mvn dependency:copy-dependencies
会导出到targed/dependency 下面
可以在工程创建lib文件夹,输入以下命令:
mvn dependency:copy-dependencies -DoutputDirectory=lib
这样jar包都会copy到工程目录下的lib里面
可以设置依赖级别,通常用编译需要的jar
mvn dependency:copy-dependencies -DoutputDirectory=lib
-DincludeScope=compile
查看一个插件的常用命令描述:
mvn help:describe -Dplugin=插件名字
mvn help:describe -Dplugin=dependency
可以查看dependency插件的常用命令。
查看一个插件的常用命令以及完整描述:
mvn help:describe -Dplugin=插件名字 -Dfull
mvn help:describe -Dplugin=dependency -Dfull
查看dependency插件的完整命令描述
创建一个最简单的maven项目:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
-DgroupId=com.tch.test -DartifactId=simpleMavenProject
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany.app -DartifactId=my-webapp
maven下载依赖jar包的源码和javadoc:
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc
maven多模块示例:
假设在名字为simple-parent的文件夹下面有一个pom.xml (也就是所谓的父模块pom.xml)、一个名字为 firstChild 的文件夹(里面是一个maven子模块)、一个名字为secondChild的文件夹(里面也是一个maven子模块)
其中父模块pom.xml 内容为:
注意:模块的加载顺序和该模块在父模块的pom.xml中配置的顺序有关,例如下面的firstChild就会先于secondChild模块
&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/maven-v4_0_0.xsd"&
&modelVersion&4.0.0&/modelVersion&
&groupId&com.tch&/groupId&
&artifactId&maven-parent&/artifactId&
&!-- 父模块的packaging要配置为pom,表明这是个单纯的pom文件 --&
&packaging&pom&/packaging&
&version&1.0&/version&
&name&Chapter 6 Simple Parent Project&/name&
&!-- 这里配置子模块的artifactId列表 --&
&module&firstChild&/module&
&module&secondChild&/module&
&/modules&
&/project&
firstChild 的pom.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.tch&/groupId&
&artifactId&maven-parent&/artifactId&
&version&1.0&/version&
&!-- 这里可以省去groupId的配置,因为该配置与父模块的groupId一样,所以可省略 --&
&artifactId&firstChild&/artifactId&
&version&1.0&/version&
&packaging&jar&/packaging&
&properties&
&project.build.sourceEncoding&UTF-8&/project.build.sourceEncoding&
&/properties&
&/project&
secondChild 模块的pom.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/maven-v4_0_0.xsd"&
&modelVersion&4.0.0&/modelVersion&
&!-- 这里配置父模块的坐标 --&
&groupId&com.tch&/groupId&
&artifactId&maven-parent&/artifactId&
&version&1.0&/version&
&!-- 这里可以省去groupId的配置,因为该配置与父模块的groupId一样,所以可省略 --&
&artifactId&secondChild&/artifactId&
&packaging&war&/packaging&
&version&1.0&/version&
&properties&
&project.build.sourceEncoding&UTF-8&/project.build.sourceEncoding&
&/properties&
&finalName&webmaven&/finalName&
&!-- 配置jetty --&
&groupId&org.mortbay.jetty&/groupId&
&artifactId&maven-jetty-plugin&/artifactId&
&/plugins&
&/project&
在父pom.xml中配置 dependency 和 配置 dependencyManagement 的区别:
在父pom.xml中配置dependency的话,子模块就会继承该依赖,假如父pom.xml中配置了下面的依赖:
&dependency&
&groupId&junit&/groupId&
&artifactId&junit&/artifactId&
&version&3.8.1&/version&
&scope&test&/scope&
&/dependency&
然后在子模块运行下面命令查看子模块的依赖:
mvn dependency:resolve
就会发现子模块的依赖包含了上面再父pom.xml中配置的依赖。类似于java里面继承的效果。
如果在父pom.xml中配置dependencyManagement的话,假如dependencyManagement里面配置了上面同样的依赖:
&dependencyManagement&
&dependencies&
&dependency&
&groupId&junit&/groupId&
&artifactId&junit&/artifactId&
&version&3.8.1&/version&
&scope&test&/scope&
&/dependency&
&/dependencies&
&/dependencyManagement&
然后在子模块运行
mvn dependency:resolve
查看子模块的依赖的话,就会发现子模块并没有继承父模块dependencyManagement里面配置的依赖。
要想使用父模块dependencyManagement里面配置的依赖,需要在子模块配置(注意没有version):
&dependency&
&groupId&junit&/groupId&
&artifactId&junit&/artifactId&
&/dependency&
,然后在子模块运行
mvn dependency:resolve
命令查看子模块的依赖,就会发现子模块的依赖包含了junit,并且版本号是父pom.xml里面配置的版本号。
所以,dependencyManagement 其实是为了统一管理版本号,把公用的依赖放到dependencyManagement里面管理,在需要引用该依赖的地方只需要groupId和artifactId即可引用该依赖。避免了版本号不同引发的问题
使用jetty插件运行web项目:
在pom.xml 的build 中添加jetty插件:(版本号自定)
&groupId&org.mortbay.jetty&/groupId&
&artifactId&maven-jetty-plugin&/artifactId&
&version&${jetty-version}&/version&
然后运行命令:mvn jetty:run 即可
貌似如果运行tomcat插件的话,我这里直接运行
mvn tomcat:run
就可以了,没有配置tomcat插件。。。
使用Maven CXF插件根据WSDL生成java类:
&groupId&org.apache.cxf&/groupId&
&artifactId&cxf-codegen-plugin&/artifactId&
&version&2.7.8&/version&
&executions&
&execution&
&id&generate-sources&/id&
&phase&generate-sources&/phase&
&configuration&
&sourceRoot&src/cxf&/sourceRoot&
&wsdlOptions&
&wsdlOption&
&wsdl&http://xxx?wsdl&/wsdl&
&frontEnd&jaxws21&/frontEnd&
&faultSerialVersionUID&1&/faultSerialVersionUID&
&/wsdlOption&
&/wsdlOptions&
&/configuration&
&goal&wsdl2java&/goal&
&/execution&
&/executions&
&/plugins&
然后执行 mvn generate-sources 即可在src/cxf目录下生成java类
将项目依赖的jar包的源码下载到目录dependency_src目录,但不解压(以jar格式):
mvn dependency:copy-dependencies -DoutputDirectory=dependency_src
-DincludeScope=compile -Dclassifier=sources
将项目依赖的jar包的源码下载到目录dependency_src目录并解压:
mvn dependency:unpack-dependencies -Dclassifier=sources -DfailOnMissingClassifierArtifact=false -DoutputDirectory=dependency_src -DincludeScope=compile
使用jacoco生成测试覆盖率report :
maven tomcat plugin : 在pom.xml中添加Plugin:然后运行:mvn tomcat7:run即可
&finalName&ssh&/finalName&
&groupId&org.apache.tomcat.maven&/groupId&
&artifactId&tomcat7-maven-plugin&/artifactId&
&version&2.2&/version&
&configuration&
&!-- http port --&
&port&9090&/port&
&!-- application path always starts with / --&
&path&/&/path&
&/configuration&
&/plugins&
maven jetty plugin:在pom.xml中添加plugin,然后运行: mvn jetty:run 即可
&groupId&org.mortbay.jetty&/groupId&
&artifactId&maven-jetty-plugin&/artifactId&
&version&6.1.10&/version&
&configuration&
&scanIntervalSeconds&5&/scanIntervalSeconds&
&webAppConfig&
&contextPath&/&/contextPath&
&/webAppConfig&
&connectors&
&connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"&
&!-- jetty port --&
&port&18080&/port&
&/connector&
&/connectors&
&/configuration&
多个module的时候,我们修改了某个module,然后只需要从该module开始build即可,这样就不必要build这个module之前的那些module了:()
mvn clean install -rf
module-name
parent pom.xml和child pom.xml:
parent pom.xml:
&module&my-modules/my-module-1&/module&
&/modules&
The value of &module& is the relative path from the com.mycompany.app:my-app:1 to com.mycompany.app:my-module:1's POM(可以在中搜索这句话)
module的值,是从parent的pom.xml到该module的pom.xml的相对路径
child pom.xml:
&groupId&parent-groupId&/groupId&
&artifactId&parent-artifactId&/artifactId&
&version&parent-version&/version&
&relativePath&../../pom.xml&/relativePath&
relativePath的值,是从该module的pom.xml到parent的pom.xml的相对路径
国内maven 仓库地址:
&?xml version="1.0" encoding="UTF-8"?&
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.
See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.
The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.
See the License for the
specific language governing permissions and limitations
under the License.
| This is the configuration file for Maven. It can be specified at two levels:
1. User Level. This settings.xml file provides configuration for a single user,
and is normally provided in ${user.home}/.m2/settings.xml.
NOTE: This location can be overridden with the CLI option:
-s /path/to/user/settings.xml
2. Global Level. This settings.xml file provides configuration for all Maven
users on a machine (assuming they're all using the same Maven
installation). It's normally provided in
${maven.home}/conf/settings.xml.
NOTE: This location can be overridden with the CLI option:
-gs /path/to/global/settings.xml
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
&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"&
&!-- localRepository
| The path to the local repository maven will use to store artifacts.
| Default: ${user.home}/.m2/repository
&localRepository&/path/to/local/repo&/localRepository&
&localRepository&D:/Program Files/apache-maven-3.3.1/repository&/localRepository&
&!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
| Default: true
&interactiveMode&true&/interactiveMode&
&!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
| Default: false
&offline&false&/offline&
&!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
&pluginGroups&
&!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
&pluginGroup&com.your.plugins&/pluginGroup&
&/pluginGroups&
&!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
&!-- proxy
| Specification for one proxy, to be used in connecting to the network.
&id&optional&/id&
&active&true&/active&
&protocol&http&/protocol&
&username&proxyuser&/username&
&password&proxypass&/password&
&host&proxy.host.net&/host&
&port&80&/port&
&nonProxyHosts&local.net|&/nonProxyHosts&
&/proxies&
&!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
&!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
used together.
&id&deploymentRepo&/id&
&username&repouser&/username&
&password&repopwd&/password&
&!-- Another sample, using keys to authenticate.
&id&siteServer&/id&
&privateKey&/path/to/private/key&/privateKey&
&passphrase& leave empty if not used.&/passphrase&
&/servers&
&!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
&!-- mirror | Specifies a repository mirror site to use instead of a given
repository. The repository that | this mirror serves has an ID that matches
the mirrorOf element of this mirror. IDs are used | for inheritance and direct
lookup purposes, and must be unique across the set of mirrors. | &mirror&
&id&mirrorId&/id& &mirrorOf&repositoryId&/mirrorOf& &name&Human Readable
Name for this Mirror.&/name& &url&/repo/path&/url&
&/mirror& --&
&id&CN&/id&
&name&OSChina Central&/name&
&url&http://maven.oschina.net/content/groups/public/&/url&
&mirrorOf&central&/mirrorOf&
&id&ibiblio&/id&
&name&ibiblio&/name&
&url&http://mirrors.ibiblio.org/pub/mirrors/maven2/&/url&
&mirrorOf&central&/mirrorOf&
&id&jboss-public-repository-group&/id&
&name&JBoss Public Repository Group&/name&
&url&http://repository.jboss.org/nexus/content/groups/public&/url&
&mirrorOf&central&/mirrorOf&
&/mirrors&
&!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
repositories, plugin repositories, and free-form properties to be used as configuration
variables for plugins in the POM.
&profiles&
&!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via &activatedProfiles/&
| or the command line, profiles have to have an ID that is unique.
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
&id&jdk-1.4&/id&
&activation&
&jdk&1.4&/jdk&
&/activation&
&repositories&
&repository&
&id&jdk14&/id&
&name&Repository for JDK 1.4 builds&/name&
&url&/maven/jdk14&/url&
&layout&default&/layout&
&snapshotPolicy&always&/snapshotPolicy&
&/repository&
&/repositories&
&/profile&
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
| &plugin&
&groupId&org.myco.myplugins&/groupId&
&artifactId&myplugin&/artifactId&
&configuration&
&tomcatLocation&${tomcatPath}&/tomcatLocation&
&/configuration&
| &/plugin&
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
anything, you could just leave off the &value/& inside the activation-property.
&id&env-dev&/id&
&activation&
&property&
&name&target-env&/name&
&value&dev&/value&
&/property&
&/activation&
&properties&
&tomcatPath&/path/to/tomcat/instance&/tomcatPath&
&/properties&
&/profile&
&/profiles&
&!-- activeProfiles
| List of profiles that are active for all builds.
&activeProfiles&
&activeProfile&alwaysActiveProfile&/activeProfile&
&activeProfile&anotherAlwaysActiveProfile&/activeProfile&
&/activeProfiles&
&/settings&
slf4j依赖:
&dependency&
&groupId&org.slf4j&/groupId&
&artifactId&slf4j-log4j12&/artifactId&
&version&1.7.2&/version&
&/dependency&
dreamoftch
浏览: 224292 次
来自: 上海
作者你好,你这个例子中服务端是跑在tomcat中吗
作者你好,能把整个项目给我发一份吗?guotao114@126 ...
楼上 本文提供的代码样例
和 你之前提供的例子有出入好多都没 ...
dannielhome 写道请问下这个upload.exe是什 ...
请问下这个upload.exe是什么东西呢话说还是maven工程,关于包的导入与本地和远程仓库的问题【java吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:655,071贴子:
话说还是maven工程,关于包的导入与本地和远程仓库的问题收藏
最近准备自己建项目,然后用了maven工程,他们说maven工程经常用不需要经常去管理以前用过的包,所以。。。maven工程有个settings.xml文件进行配置远程仓库和本地仓库。像spring框架包人家官网上有给maven的远程仓库配置以及pom.xml中的配置,所以复制过来有网就能直接下载到本地maven仓库中,并且导入到我配置过的工程里。但是像struts2 官网上没有给maven仓库以及配置,所以我就下下来了。但是问题就来了,我不知道我下载下来的那些jar包怎么导入到工程中才能跟从远程仓库中下载的一样,在本地仓库也会有一份,并且能用。如果答案是必须要先配置远程仓库,想知道,怎么通过tomcat配置多端口访问,因为web项目本身需要一个8080端口,而我只有一台电脑,我先让自己的电脑模拟服务器的方式,配置远程仓库,并且远程仓库中的文件的存放有什么要求吗?
2017java学习来上市公司博为峰学java,入学即签就业协议,不就业不收费,查看java课程!java好学吗?java课程来博为峰学,java工程师就业年薪十几万!
mvn install -groupid=x 好像是这样
好吧我表示我方向完全不对,maven他有自己的服务管理软件 nexus,经人指点,已经没什么问题了。
登录百度帐号推荐应用}

我要回帖

更多关于 maven仓库缺少jar包 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信