您的位置:首页 > 其它

maven使用的总结

2011-11-29 13:53 204 查看
对maven的使用做一些总结:

如果两个maven项目在一个工作空间下,如项目A和项目B,其中A依赖于B,则在B项目打开(非关闭)的情况下,A项目实际依赖的是工作空间下的B,而不是本地仓库里的B,此时,对B项目所作的任何修改,A项目都可以感知得到,并且在做代码跟踪的时候,可以看到此时的源代码指向的就是工作空间下的B。作为一个项目组的成员,如果大家都有这两个项目在eclipse的工作空间中,则只需更新svn即可,无需上传B项目到中央仓库。但是在B项目关闭的情况下,A项目会去本地的maven仓库获取对B项目的依赖,如果找不到,会提示编译错误。此时做代码跟踪的话,eclipse会去本地的maven仓库中寻找源代码。

如果A项目依赖于B项目,但是没有B项目的原始工程,则B项目就必须发布到中央仓库,然后,A项目通过maven update把B项目下载到本地仓库,才可以使用。

现在,说一下项目的发布。

第一步:一个项目如果需要发布到中央仓库,需要在pom.xml文件中配置如下信息

<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://10.58.120.19:8058/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>Snapshots</id>
<name>Internal Snapshots</name>
<url>http://10.58.120.19:8058/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>


主要是需要知道公司的maven中央仓库的IP地址。

第二步:发布项目到本地仓库



选中待发布的项目,右手键单击,选中"Run As","Maven install",就可以把项目打包并发布到本地的maven仓库中。

第三步:发布项目到中央仓库

项目发布到本地仓库后,只能让自己使用,不能让其他用户使用,所以,还需要继续发布到公司的中央仓库。

如图



在菜单中选中“Run”,"Run Configuration",打开运行配置界面:



单击左侧菜单中的"Maven Build",在Name中给项目起一个名字,在"Base directory"中选中待发布的项目,在“Goals”中输入maven的命令:deploy,保存,运行,可以在maven的命令行输出中看到jar包已经被上传到服务器上了



不明白maven为什么不把deploy命令集成到右键菜单上。

如果想要把本项目的javadoc也发布出去,则需要如下插件:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<charset>UTF-8</charset>
<docencoding>UTF-8</docencoding>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


<!-- 跳过测试 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<configuration>

<skip>true</skip>

</configuration>

</plugin>

<!--把项目打成可直接运行的jar包-->

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cn.vsp.TestMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>

在linux下,直接java -jar jar名称

mvn package的jar包中带有时间戳,解决办法:在pom.xml中的build任务中添加如下插件:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>


添加完插件后,mvn package后,target/lib中的jar包将不带时间戳
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: