如何使用Maven在Netbeans上创建/使用类库? [英] How do I create/use a class library with Netbeans on top of Maven?

查看:109
本文介绍了如何使用Maven在Netbeans上创建/使用类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个愚蠢的问题,但是我有点依赖Netbeans 7.1向导.似乎没有选择的办法.

This seems like a stupid question, but I am sort of dependent on Netbeans 7.1 wizards. There doesn't seem to be an option to do this.

我的Web应用程序是使用Maven构建的,我想将其某些软件包分解为一个独立构建和维护的类库(特别是JPA部分,但这无关紧要.)然后,我想删除那些软件包.脱离原始Web应用程序,然后创建对我创建的工件的依赖关系.

My web-app is built with Maven and I want to break out some of its packages into an independently built and maintained class library (specifically, the JPA part but that shouldn't matter.) Then I want to delete those packages out of the original web application and then create a dependency to the artifact that I create.

那我该如何开始呢? Netbeans新建项目向导似乎没有像ANT生成的项目那样为"Java类库"提供选项.我看到的最接近的选择是"Java应用程序".那是我所使用的,只是忽略了主类,还是有其他我看不到的路径?

So how do I get started? The Netbeans New-Project wizard doesn't seem have an option for "Java Class Library" like it does for ANT-built projects. The closest choice I can see is "Java Application." Is that what I use and just ignore the main class or is there some other path I don't see?

我通常很擅长收集这些东西,但是我的网络搜索却收效甚微.非常感谢您的帮助.

I am usually pretty good at picking this stuff up but my web searches aren't yielding much. Much thanks for any help.

推荐答案

就maven而言,您可以将向导用于"Maven"->"Java应用程序(使用Maven的简单Java SE应用程序)".库和应用程序之间没有区别.我认为netbeans将创建一个示例App.java,您可以将其删除.

You can use the wizard for "Maven" -> "Java Application (A simple Java SE application using Maven)", as far as maven is concerned there is no difference between a libary and an application. I think netbeans will create a sample App.java that you can simply delete.

对于您的用例,还应该为库和Web应用程序创建一个父项目.然后,构建父对象也将同时构建库和Web应用程序.它还允许您在Web应用程序上使用使用依赖项进行构建",并首先重建该库.

For your usecase it would make sense to also create a parent project for the library and the webapplication. Building the parent would then also build both the library and web application. It also allows you to use "build with dependencies" on the web application and have the library rebuilt first.

要创建父项目,可以使用"POM项目"条目.理想情况下,目录结构如下:

To create a parent project you can use the "POM Project" entry. The directory structure ideally looks like this:

- pom.xml (for parent project)
- library (folder)
    - pom.xml (for library module)
- webapp  (folder)
    - pom.xml (for webapp module)

然后,父项目应包含module元素,其中应包含其他项目的相对路径:

The parent project should then contain module elements containing the relative path of your other projects:

<modules>
    <module>library</module>
    <module>webapp</module>
</modules>

库和Web应用程序这样引用父级,默认将groupId和版本与父级相同:

The library and web application reference the parent like this, the groupId and version are defaulted to be the same as the parent:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>myGroupId</groupId>
    <artifactId>parentArtifactId</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>
<artifactId>libraryArtifactId</artifactId>

这篇关于如何使用Maven在Netbeans上创建/使用类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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