maven添加远程仓库以及导出依赖的jar包以及常用操作收集
来源:互联网 发布:网络摄像机哪个好 编辑:程序博客网 时间:2024/06/06 01:32
http://dreamoftch.iteye.com/blog/1976794
Maven常见的问题集锦
先说结果吧,要添加其它的远程仓库,需要在maven的conf目录下的setting.xml里面添加下面配置:
在 <profiles> 节点下添加(里面的url地址就是仓库的地址,根据自己的情况替换就好了):
- <profile>
- <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最佳实践:Maven仓库
从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
参考:
Maven导出工程依赖的jar包
查看一个插件的常用命令描述:
- 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
maven web项目:
- 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>
- <modules>
- <!-- 这里配置子模块的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>
- <!-- 这里配置父模块的坐标 -->
- <parent>
- <groupId>com.tch</groupId>
- <artifactId>maven-parent</artifactId>
- <version>1.0</version>
- </parent>
- <!-- 这里可以省去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>
- <!-- 这里配置父模块的坐标 -->
- <parent>
- <groupId>com.tch</groupId>
- <artifactId>maven-parent</artifactId>
- <version>1.0</version>
- </parent>
- <!-- 这里可以省去groupId的配置,因为该配置与父模块的groupId一样,所以可省略 -->
- <artifactId>secondChild</artifactId>
- <packaging>war</packaging>
- <version>1.0</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <build>
- <finalName>webmaven</finalName>
- <plugins>
- <!-- 配置jetty -->
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>maven-jetty-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </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插件:(版本号自定)
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>maven-jetty-plugin</artifactId>
- <version>${jetty-version}</version>
- </plugin>
然后运行命令:mvn jetty:run 即可
貌似如果运行tomcat插件的话,我这里直接运行 mvn tomcat:run 就可以了,没有配置tomcat插件。。。
使用Maven CXF插件根据WSDL生成java类:
- <build>
- <plugins>
- <plugin>
- <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>
- <goals>
- <goal>wsdl2java</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
然后执行 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 :
参考:http://www.eclemma.org/jacoco/trunk/doc/index.html
- Maven Usage Example - Offline Example
maven tomcat plugin : 在pom.xml中添加Plugin:然后运行:mvn tomcat7:run即可
- <build>
- <finalName>ssh</finalName>
- <plugins>
- <plugin>
- <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>
- </plugin>
- </plugins>
- </build>
maven jetty plugin:在pom.xml中添加plugin,然后运行: mvn jetty:run 即可
- <plugin>
- <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>
- </plugin>
多个module的时候,我们修改了某个module,然后只需要从该module开始build即可,这样就不必要build这个module之前的那些module了:(详细参考)
- mvn clean install -rf module-name
parent pom.xml和child pom.xml:
parent pom.xml:
- <modules>
- <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:
- <parent>
- <groupId>parent-groupId</groupId>
- <artifactId>parent-artifactId</artifactId>
- <version>parent-version</version>
- <relativePath>../../pom.xml</relativePath>
- </parent>
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.
- |-->
- <proxies>
- <!-- proxy
- | Specification for one proxy, to be used in connecting to the network.
- |
- <proxy>
- <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|some.host.com</nonProxyHosts>
- </proxy>
- -->
- </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.
- |-->
- <servers>
- <!-- 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.
- |
- <server>
- <id>deploymentRepo</id>
- <username>repouser</username>
- <password>repopwd</password>
- </server>
- -->
- <!-- Another sample, using keys to authenticate.
- <server>
- <id>siteServer</id>
- <privateKey>/path/to/private/key</privateKey>
- <passphrase>optional; leave empty if not used.</passphrase>
- </server>
- -->
- </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.
- |-->
- <mirrors>
- <!-- 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>http://my.repository.com/repo/path</url>
- </mirror> -->
- <mirror>
- <id>CN</id>
- <name>OSChina Central</name>
- <url>http://maven.oschina.net/content/groups/public/</url>
- <mirrorOf>central</mirrorOf>
- </mirror>
- <mirror>
- <id>ibiblio</id>
- <name>ibiblio</name>
- <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
- <mirrorOf>central</mirrorOf>
- </mirror>
- <mirror>
- <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>
- </mirror>
- </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.
- <profile>
- <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>http://www.myhost.com/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.
- |
- <profile>
- <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>
添加log4j slf4j依赖:
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.2</version>
- </dependency>
阅读全文
0 0
- maven添加远程仓库以及导出依赖的jar包以及常用操作收集
- Maven常用仓库地址以及手动添加jar包到仓库
- Maven常用仓库地址以及手动添加jar包到仓库
- Maven常用仓库地址以及手动添加jar包到仓库
- Maven常用仓库地址以及手动添加jar包到仓库
- Maven常用仓库地址以及手动添加jar包到仓库
- Maven常用仓库地址以及手动添加jar包到仓库
- jar包本地安装以及常用maven仓库
- 常用开源jar包的maven坐标以及常用仓库地址
- 从Maven仓库中导出 依赖 jar包
- maven 导出jar包以及导出dubbo服务jar包
- maven导出项目依赖的jar包
- maven导出项目依赖的jar包
- Maven导出Project依赖的jar包
- maven导出项目依赖的jar包
- maven导出依赖的jar包
- Maven导出Project依赖的jar包
- maven导出项目依赖的jar包
- 点击按钮从上而下弹出漂亮的模拟框
- 模块化物联网应用开发方式( 第 1 部分)硬件选择---凯利讯半导体
- Google中国开发者大会落幕,我们细思一下这次开发者大会的干货是什么?
- 浅析LinkedList
- 安卓调用系统相机拍照的几个步骤,同时解决版本兼容问题
- maven添加远程仓库以及导出依赖的jar包以及常用操作收集
- IT风投:狂袭之风
- sed之妙用
- KeepAlived实现高可用(suse)
- 浅析HashMap
- 浅析ArrayList
- SSM项目从零开始到入门009-使用mybatis提供的动态代理
- Java多线程原理及使用探讨
- webstrom识别 React语法