使用外部jar文件中的类编译Maven项目(不在Maven工件中) [英] Compiling a maven project with class from external jar file (that is not in a maven artifact)

查看:238
本文介绍了使用外部jar文件中的类编译Maven项目(不在Maven工件中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java有点陌生,对maven也很陌生.我可能不完全使用某些术语.

在开发使用maven进行构建的项目时,由于需要的类不是核心Java语言的一部分,所以在pom.xml中添加了相应的<dependency>部分(通常是从mvnbrowser.com复制而来,但我并不太了解),而且我已经能够使用想要的类.效果很好.

现在,我遇到了我想使用的Java类,该类显然在Maven工件中不可用.它是com.android.sdklib.build.ApkBuilder,与android SDK一起分发.我编写了一个独立的Java程序来使代码正常工作.我用javac而不是maven编译它,并且工作正常.为此,我将包含所需类sdklib.jar的jar文件放入我的jre/lib/ext目录(在FreeBSD机器上的/usr/local/jdk1.6.0/jre/lib/ext)中,并且import com.android.sdklib.build.ApkBuilder行可以正常编译,我可以参考并毫无问题地使用它.

现在,当我将工作代码复制到我的maven项目中,并尝试使用mvn project进行构建时,在导入行中出现package com.android.sdklib.build does not exist错误,而在引用ApkBuilder时出现cannot find symbol: class ApkBuilder错误. /p>

环顾四周,我在这里找到android-maven-plugin: http://code .google.com/p/maven-android-plugin/.它在这里将我引到maven-android-sdk-deployer: https://github.com/mosabua/maven- android-sdk-deployer .这些线索起初听起来很有希望,但据我所知,它们的目的是使用maven开发android应用程序,这不是我要尝试做的.我只希望我的Java程序能够操纵android apk文件,但是我的Java程序本身不能在android设备上运行.另外,android SDK可以与linux一起使用,而不是与我正在使用的FreeBSD一起使用,因此我无法遵循maven-android-sdk-deployer的安装说明.我还发现了一个名为maven-apkbuilder-plugin的工件,但这似乎也是出于创建android应用程序的目的,我没有看到有关它的文档.因此,我没有追求这些领先优势.

似乎最简单的事情就是让编译器在运行mvn package时在我的jre/lib/ext文件夹中查找sdklib.jar文件.但这也许不是要走的路.也许我可以对pom.xml进行简单的更改,使编译器找到sdklib.jar.我在 http://maven.apache.org/pom.html#Build_Settings <上查看了pom文档. /a>,但没有找到一种方法.无论如何,我敢肯定至少有一种解决方案.我只是还不知道.

我有什么选择?在我的Maven构建的项目中使用ApkBuilder类的最佳方法是什么?我应该读什么书,才能使我自己回答这个问题?谢谢!

解决方案

如果您只是尝试引用jar,则最简单的解决方案可能是手动将jar添加到本地存储库中.有关详细信息,请参见此处.

果壳:

mvn install:install-file       \
    -Dfile=<path-to-file>      \
    -DgroupId=<group-id>       \
    -DartifactId=<artifact-id> \
    -Dversion=<version>        \
    -Dpackaging=jar

您可以 使用group-idartifact-idversion的任何内容,但是它们应该类似于您在存储库中可用的内容.

由于您实际上并不是在创建Android项目,所以这可能就足够了.


这就是我要问的问题.

如何使用我的Maven构建中的Maven存储库无法提供的库?

(巧合的是,它以及它的较短变体应该显示出您应该"阅读的链接,以回答您的最后一个问题:)

I'm somewhat new to java, and very new to maven. I might use some terminology imperfectly.

As I develop my project that I'm using use maven to build, as I've needed classes that aren't part of the core java language I've added an appropriate <dependency> section to my pom.xml (usually copied from mvnbrowser.com without my really understanding it very well), and I've been able to use the classes I want. Works great.

Now I've encountered a java class that I want to use that is apparently not available in a maven artifact. It's com.android.sdklib.build.ApkBuilder which is distributed with the android SDK. I wrote a standalone java program to get my code working; I compile it with javac and not with maven, and that works fine. For that, I put the jar file that contains the class I want, sdklib.jar, into my jre/lib/ext directory (/usr/local/jdk1.6.0/jre/lib/ext on a FreeBSD machine), and my import com.android.sdklib.build.ApkBuilder line compiles fine and I can refer to ApkBuilder and use it with no problems.

Now, when I copied my working code into my maven project, and tried to build with mvn project, I get package com.android.sdklib.build does not exist errors on the import line, and cannot find symbol: class ApkBuilder errors where I refer to ApkBuilder.

Looking around the net, I found android-maven-plugin here: http://code.google.com/p/maven-android-plugin/. It referred me to maven-android-sdk-deployer here: https://github.com/mosabua/maven-android-sdk-deployer. These leads sounded promising at first, but as far as I can tell, their purpose is for developing android applications using maven, which is not what I'm trying to do. I just want my java program to manipulate android apk files, but my java program itself is not to be run on an android device. Also, the android SDK is made to work with linux, not FreeBSD which I'm using, so I cannot follow the installation instructions for maven-android-sdk-deployer. I also found an artifact called maven-apkbuilder-plugin but that also seems to be for the purpose of creating android applications and I didn't see documentation for it. So I haven't pursued these leads.

It seems the easiest thing would be just to get the compiler to look in my jre/lib/ext folder for the sdklib.jar file when running mvn package. But maybe that's not the way to go. Or maybe there's a simple change I can make to my pom.xml that will cause the compiler to find sdklib.jar. I looked at the pom documentation at http://maven.apache.org/pom.html#Build_Settings, but didn't see a way to do that. Anyway, I'm sure there's at least one solution; I just don't know it (yet).

What are my options? What's the best way to use the ApkBuilder class in my maven-built project? What should I have read that would have enabled me to answer this question myself? Thanks!!

解决方案

If you're just trying to reference the jar, the simplest solution might be to add the jar to your local repository manually. See here for details.

Nutshell:

mvn install:install-file       \
    -Dfile=<path-to-file>      \
    -DgroupId=<group-id>       \
    -DartifactId=<artifact-id> \
    -Dversion=<version>        \
    -Dpackaging=jar

You may use whatever you want for the group-id, artifact-id, and version, but they should be something resembling what you'd expect if it was available in the repo.

Since you're not actually creating an Android project, this may be sufficient.


Here's how I would have asked the question.

How can I use a library not available from a Maven repository in my Maven build?

(Coincidentally, that, and shorter variations of that, should bring up the links you "should" have read, to answer your last question :)

这篇关于使用外部jar文件中的类编译Maven项目(不在Maven工件中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆