是否可以设置一个 IntelliJ Android 项目来与 Maven 一起使用?
我想在 IntelliJ Android 项目中使用 Maven 依赖项。有人成功做到这一点吗?
I want to work with Maven dependencies in an IntelliJ Android project. Has anybody successfully done this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Ubuntu 11.10 和 Mac OS X 10.7.3 上使用 IntelliJ IDEA(当前 v11.0.2,内部版本 111.277)来处理基于 Maven 的 Android 项目,并且在大多数情况下它运行良好。将 IntelliJ 与 Android 和 Maven 结合使用的一个好处是所有支持都已内置 - 无需安装额外的 IDE 插件。
作为可以使用 IntelliJ 打开的良好运行的 Android Maven 项目的示例,您可以使用开源 Gaug.xml 的源代码。 GitHub 上的 es for Android 应用程序(并非 GitHub 上的所有开发人员都使用 IntelliJ,我个人也使用 IntelliJ - Eclipse 也用于处理此项目)。
平稳运行的第一步显然是确保您的项目在命令行中仅使用普通 Maven 正确构建 - 尽管问题没有直接询问这一点,但我建议至少具备这些先决条件:
如果你可以
mvn install
你的项目的父pom,你就可以继续前进了实际上与 IntelliJ 一起使用它。项目结构
、平台设置
、SDK
,然后编辑 Java SDK(如果它显示为红色),为其提供 Java 6 SDK 的路径。Project Structure
对话框窗口中,将Project SDK
设置为适当的 Android 版本,并在Project
下设置Project 编译器输出
为您喜欢的任何目录名称 - 这个值是执行“Make”所必需的,但也可以被您的子模块覆盖以明智地指向 Maven“目标”目录。Maven Projects
选项卡。点击Reimport All Maven Projects
按钮(看起来像两个互相追逐的箭头)。如果 IntelliJ 提示您启用自动导入,请启用它。现在,您应该在Maven Projects
选项卡下至少列出一个模块,如果您还有集成测试项目,则还会列出更多模块。此时,您应该拥有相当有意义的 IDE 体验。应该有效的东西:
强制重新生成 R.java 文件
应该正确刷新target/ generated-sources/r/...
下的文件 - 但这似乎只有在target/ 时才有效generated-sources/r
已设置为源路径,即已由 Maven 生成,并由 IntelliJ 导入。最小的命令行替代方法是从受影响模块的文件夹中的命令行执行mvn android:generate-sources
。package
或install
调用所有构建。可能不起作用的东西:
ClassNotFoundException: junit.textui.ResultPrinter
而终止)如果 IntelliJ 感到困惑,以下步骤通常会让您回到正轨:
在命令行上 mvn clean install
重新导入所有 Maven 项目
按钮一些建议:
希望有帮助!
I use IntelliJ IDEA (currently v11.0.2, build 111.277) on both Ubuntu 11.10 and Mac OS X 10.7.3 for working on Maven-based Android projects, and for the most part it works well. A nice thing about using IntelliJ with Android and Maven is that all support is already built-in - there are no extra IDE plugins to install.
As an example of a good working Android Maven project that can be opened with IntelliJ, you could use the source code of the open-source Gaug.es for Android app by GitHub (not all developers at GitHub use IntelliJ, personally I do - Eclipse is also used to work on this project).
The first step for smooth running is obviously to ensure that your project builds correctly using only plain Maven at the command line - although the question doesn't directly ask about this, I'd advise these pre-requisites as a bare minimum:
If you can
mvn install
the parent pom of your project, you're good to move on to actually working with it with IntelliJ.Project Structure
,Platform Settings
,SDKs
and then edit the Java SDK if it's showing as red, giving it the path of your Java 6 SDK.Project Structure
dialog window, set theProject SDK
to the appropriate Android version, and underProject
set theProject compiler output
to whatever directory name you like - this value is required for doing a 'Make', but also overridden to by your submodules to sensiibly point to the Maven 'target' directories.Maven Projects
tab. Hit theReimport All Maven Projects
button (looks like two arrows chasing each other). If IntelliJ prompts you to enable Auto-Import, go for it. You should now have at least one module listed under theMaven Projects
tab, more if you have an integration test project as well.At this point, you should have a fairly meaningful IDE experience. Stuff that should work:
Force regenerate R.java file
should correctly refresh the file undertarget/generated-sources/r/...
- but this only seems to work iftarget/generated-sources/r
is already set as a source path, ie has been generated by Maven, and imported by IntelliJ. The minimal command line alternative is to executemvn android:generate-sources
from the command line in the folder of the affected module.package
orinstall
at the command line.Stuff that might not work:
ClassNotFoundException: junit.textui.ResultPrinter
)If IntelliJ gets confused, the following steps will normally get you back on track:
mvn clean install
on the command lineReimport All Maven Projects
buttonSome advice:
Hope that helps!
请参阅maven-android-plugin 入门。
您可以使用这里的命令来创建具有 Maven 结构的示例工作模板:
顺便说一句,IntelliJ 内置了对 Maven 的支持,因此只需从中打开 pom.xml - 一切都会好起来的。
See Getting Started with the maven-android-plugin.
Here the command you can use to create an sampe working template with Maven structure:
BTW, IntelliJ has built in support for maven, so just open pom.xml from it - and everything will be ok.