Maven用仓库外的jar进行编译
http://blog.csdn.net/zhouysh/article/details/5713532
之所以采用Maven调用ant 进行编译,是因为有一些lib 并不在远程仓库里,而是直接放到project/lib目录下。
编译的时候要依赖这些lib包, maven里好像没什么办法把这些非仓库里的lib包加入到classpath中来,才采用调用ant的方式。
ps: 但是如果要采用这种把非仓库lib的加入到classpath的方式,就无法使用maven官方推荐的maven-ant-tasks进行抽取pom的dependency,所以我在这个例子中把那段注释掉了。。
这是不是一个maven的bug?
----------------------------------------------------------------------------
Pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>ant-build</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<property name="compile_classpath" refid="maven.compile.classpath" />
<property name="runtime_classpath" refid="maven.runtime.classpath" />
<property name="test_classpath" refid="maven.test.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<property name="artifactId" value="${project.artifactId}" />
<property name="version" value="${project.version}" />
<property name="build.compiler" value="extJavac"/>
<ant antfile="build.xml" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
-------------------------------------------------------------------------------------------------------------------
build.xml
<project name="uslaminstallerjavaproject" default="dist" basedir="." xmlns:artifact="urn:maven-artifact-ant">
<property environment="env" />
<property name="vendor" value="HP Company." />
<property name="work.src" value="${basedir}/src/main/java" />
<property name="work.conf" value="${basedir}/src/main/resources" />
<property name="work.lib" value="${basedir}/lib" />
<property name="work.dist" value="${basedir}/target" />
<property name="work.classes" value="${basedir}/target/classes" />
<echo message="compile classpath: ${compile_classpath}" />
<echo message="runtime classpath: ${runtime_classpath}" />
<echo message="test classpath: ${test_classpath}" />
<echo message="plugin classpath: ${plugin_classpath}" />
<target name="clean" description="Delete old build and dist directories">
<echo message=" Clean the classe directory" />
<delete dir="${work.classes}" />
<delete dir="${work.dist}" />
</target>
<target name="mkdir" description="Make build and dist directories">
<echo message=" Make the classe directory" />
<mkdir dir="${work.classes}" />
<mkdir dir="${work.dist}" />
</target>
<!-- 但是如果要采用这种把非仓库lib的加入到classpath的方式,就无法使用maven官方推荐的maven-ant-tasks进行抓取dependency -->
<!--
<target name="maven-jar" description="Use Maven2 to manage jars' dependencies">
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
<classpath>
<pathelement location="lib/maven-ant-tasks-2.0.9.jar" />
</classpath>
</typedef>
<artifact:pom id="maven.project" file="pom.xml" />
<echo message="The build directory is ${maven.project.build.directory}" />
<artifact:dependencies pathId="maven.classpath" filesetId="maven.deps.fileset" usescope="runtime">
<pom refid="maven.project" />
</artifact:dependencies>
</target>
-->
<path id="project.classpath">
<dirset dir="${work.conf}"/>
<fileset dir="${work.lib}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
<pathelement path="${compile_classpath}"/>
<!--<fileset refid="maven.deps.fileset" /> -->
</path>
<target name="compile" description="compiles all source files">
<echo message=" Start to compile..." />
<javac srcdir="${work.src}" destdir="${work.classes}" debug="true">
<classpath refid="project.classpath" />
</javac>
<echo message=" Finish to compile..." />
</target>
<target name="jar-all" description="Jar the classes">
<jar destfile="${work.dist}/${artifactId}-${version}.jar"
basedir="${work.classes}" encoding="UTF-8">
<fileset dir="${work.conf}">
<include name="**/*.*"/>
</fileset>
<manifest>
<attribute name="Implementation-Title"
value="${artifactId}" />
<attribute name="Implementation-Version"
value="${version}" />
<attribute name="Implementation-Vendor"
value="${vendor}" />
</manifest>
</jar>
</target>
<target name ="zip-release" description="Zip the final release file">
<zip destfile="${work.dist}/${artifactId}-${version}.zip">
<zipfileset dir="${work.dist}" includes="**/*.jar"/>
<zipfileset dir="${work.lib}" includes="**/*.jar" excludes= "maven-ant-tasks*.jar" />
</zip>
</target>
<target name="dist" depends="clean,mkdir,compile,jar-all">
<antcall target="zip-release">
</antcall>
</target>
</project>
========================
另外一种就是在父项目中调用两个子模块,第一个模块去将jar安装到本地仓库,第二个子模块就可以依赖这些jar了
==========
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/lib/xxx.jar</systemPath>
</dependency>
在eclipse项目需右键选择Maven->enable dependency management
http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them
Can I add jars to maven 2 build classpath without installing them?
up vote 239 down vote favorite
134
|
Maven2 is driving me crazy during the experimentation/quick and dirty mock-up phase of development. I have a pom.xml file that defines the dependencies for the web-app framework I want to use, and I can quickly generate starter projects from that file. However, sometimes I want to link to a 3rd party library that doesn't already have a pom.xml file defined, so rather than create the pom.xml file for the 3rd party lib by hand and install it, and add the dependency to my pom.xml, I would just like to tell maven: "In addition to my defined dependencies, include any jars that are in /lib too." It seems like this ought to be simple, but if it is, I am missing something. Any pointers on how to do this are greatly appreciated. Short of that, if there is a simple way to point maven to a /lib directory and easily create a pom.xml with all the enclosed jars mapped to a single dependency which I could then name/install and link to in one fell swoop would also suffice. Thanks!
javamaven-2
|
||
feedback
|
15 Answers
up vote 217 down vote
|
set scope == system and just make up a groupId, artifactId, and version
|
||||||||||||||||||||
show6 more comments
feedback
|
up vote 133 down vote
|
Problems of popular approachesMost of the answers you'll find around the internet will suggest you to either install the dependency to your local repository or specify a "system" scope in the Why you shouldn't apply the "Install to Local Repo" approachWhen you install a dependency to your local repository it remains there. Your distribution artifact will do fine as long as it has access to this repository. The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. Clearly making your artifact depend on a specific machine is not a way to handle things. Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better. Why you shouldn't apply the "System Scope" approachThe jars you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. That's why your distribution package won't have a way to resolve that dependency when used. That I believe was the reason why the use of system scope even got deprecated. Anyway you don't want to rely on a deprecated feature. The static in-project repository solutionAfter putting this in your
for each artifact with a group id of form
To elaborate more on this you can read this blog post. Use Maven to install to project repoInstead of creating this structure by hand I recommend to use a Maven plugin to install your jars as artifacts. So, to install an artifact to an in-project repository under
If you'll choose this approach you'll be able to simplify the repository declaration in
A helper scriptSince executing installation command for each lib is kinda annoying and definitely error prone, I've created autility script which automatically installs all the jars from a Include the dependencies in your target packageWhen you'll have your in-project repository created you'll have solved a problem of distributing the dependencies of the project with its source, but since then your project's target artifact will depend on non-published jars, so when you'll install it to a repository it will have unresolvable dependencies. To beat this problem I suggest to include these dependencies in your target package. This you can do with either theAssembly Plugin or better with theOneJar Plugin. The official documentaion on OneJar is easy to grasp. |
||||||||||||||
show1 more comment
feedback
|
up vote 24 down vote
|
Note: When using the System scope (as mentioned on this page), Maven needs absolute paths. If your jars are under your project's root, you'll want to prefix your systemPath values with ${basedir}. |
feedba
|
up vote 9 down vote
|
You really ought to get a framework in place via a repository and identifying your dependencies up front. Using the system scope is a common mistake people use, because they "don't care about the dependency management." The trouble is that doing this you end up with a perverted maven build that will not show maven in a normal condition. You would be better off following an approach likethis. |
||
feedback
|
up vote 6 down vote
|
Maven install plugin has command line usage to install a jar into the local repository, POM is optional but you will have to specify the GroupId, ArtifactId, Version and Packaging (all the POM stuff). |
||||
feedback
|
up vote 5 down vote
|
This is what I have done, it also works around the package issue and it works with checked out code. I created a new folder in the project in my case I used In my POM I had a dependency that is not in any public maven repositories
I then created the following directories I created the following POM file to represent the downloaded file (this step is optional, but it removes a WARNING) and helps the next guy figure out where I got the file to begin with.
Two optional files I create are the SHA1 checksums for the POM and the JAR to remove the missing checksum warnings.
Finally I add the following fragment to my pom.xml that allows me to refer to the local repository
|
||||||
feedback
|
up vote 3 down vote
|
The problem with Then declare dependencies as usual. And please read the footer note. |
||
feedback
|
up vote 3 down vote
|
This is how we add or install a local jar
i gave some default groupId and artifactId because they are mandatory :) |
||
feedback
|
up vote 2 down vote
|
If you want a quick and dirty solution, you can do the following (though I do not recommend this for anything except test projects, maven will complain in length that this is not proper). Add a dependency entry for each jar file you need, preferably with a perl script or something similar and copy/paste that into your pom file.
|
||
feedback
|
up vote 2 down vote
|
You may create local repository on your project For example if you have
That is all. For detailed information: How to add external libraries in Maven |
||
feedback
|
up vote 1 down vote
|
Even though it does not exactly fit to your problem, I'll drop this here. My requirements were:
Let's talk about (3) first: Just having the jars in a folder and somehow merging them into the final jar will not work for here, since the IDE will not understand this. This means all libraries have to be installed properly. However, I dont want to have everyone installing it using "mvn install-file". In my project I needed metawidget. Here we go:
Every time you have a new library, just add a new execution and tell everyone to build the project again (you can improve this process with project hierachies). |
||||||||||
feedback
|
up vote 1 down vote
|
After having really long discussion with CloudBees guys about properly maven packaging of such kind of JARs, they made an interesting good proposal for a solution: Creation of a fake Maven project which attaches a pre-existing JAR as a primary artifact, running into belonged POM install:install-file execution. Here is an example of such kinf of POM:
But in order to implement it, existing project structure should be changed. First, you should have in mind that for each such kind of JAR there should be created different fake Maven project (module). And there should be created a parent Maven project including all sub-modules which are : all JAR wrappers and existing main project. The structure could be :
When parent running via mvn:install or mvn:packaging is forced and sub-modules will be executed. That could be concerned as a minus here, since project structure should be changed, but offers a non static solution at the end |
||
feedback
|
up vote 0 down vote
|
This doesn't answer how to add them to your POM, and may be a no brainer, but would just adding the lib dir to your classpath work? I know that is what I do when I need an external jar that I don't want to add to my Maven repos. Hope this helps. |
||
feedback
|
up vote 0 down vote
|
A strange solution I found: using Eclipse
cheers, Balint |
Was this post useful to you?
|
up vote 0 down vote
|
What works in our project is what Archimedes Trajano wrote, but we had in our .m2/settings.xml something like this:
and the * should be changed to central. So if his answer doesn't work for you, you should check your settings.xml |
Maven用仓库外的jar进行编译相关推荐
- maven本地仓库中存在jar包,但编译不成功,显示jar包不存在
介绍一下背景,项目要迁移进坑人的离线的内网开发,将在同事那编译通过的代码和maven仓库拷进内网,打算编译通过之后再上传私服,结果配好maven之后,本地库中的部分jar包显示没有引入,如下面的波浪线 ...
- 从Maven中央仓库网站下载jar包的两种方式,将会伴随java后端开发者的整个职业生涯
这个肥肠重要的网站就是:https://mvnrepository.com/ 下面我会以mysql-connector-java-8.0.26.jar为例,教会你使用两种方式下载使用jar包资源 首先 ...
- 发布/上传Jar包到Maven中央仓库 - 史上最详细
发布 Jar 包到 Maven 中央仓库 在项目开发过程中,我们常常会使用 Maven / Gradle 从仓库拉取开源的第三方 jar 包,可能是私有仓库,可能是 Maven 中央仓库,也可能是第三 ...
- 发布个人项目jar包到maven中央仓库详解
发布个人项目jar包到maven中央仓库详解 1.在sonatype提交发布工单(Issue) sonatype是由社区支持的开源项目托管服务(Open Source Project Reposito ...
- install package vif包_Nexus上传自己本地jar包 和下载maven中央仓库里的包到nexus - 剑器近丶...
一.上传自己本地jar包到Nexus 1)在Nexus中创建maven2hosted仓库 2) 我这里使用的是混合的() 也可以创建两个 一个是releases 一个是snapshots. 3)上传前 ...
- 我把自己的java库发布到了maven中央仓库,从此可以像Jackson、Spring的jar一样使用它了
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于maven中央仓库 作为一个java程序员,对ma ...
- 发布Jar包到Maven中央仓库(为开发开源中间件做准备)
微信公众号:bugstack虫洞栈 沉淀.分享.成长,专注于原创专题案例,以最易学习编程的方式分享知识,让自己和他人都能有所收获.目前已完成的专题有:Netty4.x实战专题案例.用Java实现JVM ...
- Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...
一,实例:新建了一个Maven项目,在eclipse中通过 build path –> configure path-.将依赖包添加到工程中后,eclipse不报错了.但是用Maven命令 mv ...
- 怎样在nexus 中 搜索到远程maven仓库中的jar 文件
怎样在nexus 中 搜索到远程maven仓库中的jar 文件 url: http://www.oschina.net/question/95712_21999 点击Administration菜单下 ...
最新文章
- web程序前后台功能实现_微信定制开发、小程序定制开发可以实现哪些功能?
- Block Formatting Context
- Traincascade Error: Bad argument (Can not get new positive sample. The most possible reason is insuf
- js将文字填充与canvas画布再转为图片
- [vue] 怎么访问到子组件的实例或者子元素?
- 线性代数拾遗(四):线性方程组的应用
- 测量坐标出现6位、7 、8位的情况
- mysql 驱动说明_mysql_jdbc连接说明
- 用vscode创建一个c项目_Visual Studio Code创建C#项目
- 蓝天集团董事长郎凤娥专访
- 【报告分享】2019年中国数字经济发展指数.pdf(附下载链接)
- 淦!这个非科班学妹是真的厉害...
- 指针 多维数组 数组指针 指针数组
- 如何让imageView铺满屏幕?
- KNN分类USPS, USI sonar及USI iris
- flea-db使用之封装JDBC接入
- 题目 A : 勇士传说
- 关于华硕笔记本BIOS设置
- TSINGSEE青犀视频构建pion webrtc运行broadcast示例的步骤
- 计算机组成原理平均cpi怎么算_计算机组成原理计算题
热门文章
- G6 图可视化引擎——入门教程——绘制 Tutorial 案例
- 11.1.1 JavaScript基本语法
- 从源代码学Python系列目录
- PaddlePaddle训练营——公开课——AI核心技术掌握——第2章机器能“看”的现代技术——源自视觉神经原理的卷积网络简介及深入理解
- SQL开发技巧 join从句
- ROS通信架构(上)
- 【工业控制】如何优化波形
- 【Linux】一步一步学Linux——dmesg命令(74)
- java image_Java 图片处理解决方案:ImageMagick 快速入门教程
- mysql中整理设置__MySQL整理