传统的开源Java应用程序 [英] Traditional Open Source Java Application

查看:139
本文介绍了传统的开源Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Java进行家庭项目,现在需要通过github或Google Code等存储库来使我的一些项目可用。

I've been using Java for my "home projects" for a while and now there's a need to make some of my projects available through the repositories like github or Google Code.

只要我是项目的唯一开发人员,只要我一直在Eclipse中工作,对构建和运行它是绝对没有问题的。一切都正常。问题基本上是,如果有人要我分享,该怎么办? : - )

As long as I'm the only developer for the project and as long as I always work in Eclipse, there are absolutely no questions about building and running it. Everything just work fine. The question is basically, what do I do if someone asks me to share? :-)

首先要做的是,使用Maven这样的工具来构建项目。在这种情况下,无论是否安装了Eclipse,或者喜欢IDEA,他都可以下载我的项目并使用Maven构建。或者,导入到Eclipse / IDEA并构建。

The first thing to do is, making the project buildable with tools like Maven. In this case, no matter if someone has Eclipse installed, or prefers IDEA, he may just download my project and build using Maven. Or, import to Eclipse/IDEA and build.

所以,为了澄清问题,这里有两点:

So, to clarify the question, here are 2 points:


  1. 我的目录结构应该怎么样?像src,bin这样的文件夹,其他什么?

  2. 应用程序的运行方式是什么?例如,我的应用程序需要log4j才能运行。构建包时,Maven可以解决这种依赖关系。但是当软件包被构建时,您应该手动提供您的路径log4j(使用 java -cp ... ) - 如果我要喜欢只提供.sh和.cmd脚本,方便用户使用?

  1. What should my directory structure look like? Folders like src, bin, anything else?
  2. What is the way my application should run? For instance, my application requires log4j in order to run. Maven resolves this dependency, when building the package. But when the package is built, you should manually provide the path to you log4j (with java -cp ...) - what's the way to eliminate this requirement if I'd like to just provide .sh and .cmd scripts for user's convenience?


推荐答案

从我的个人经验,maven是构建和管理依赖项的最佳可用工具,所以我的答案是关于maven。另外,使用maven,您只需要提供包含依赖关系和项目构建配置定义的pom.xml文件。您共享的项目不需要在其中包含任何JAR,因为它曾经是基于ant的项目。

From my personal experience, maven is the best available tool for building and managing dependencies, so my answers for your questions are all about maven. Also, with maven you need to provide only pom.xml file containing definition of dependencies and project build configuration. Project you share does not need to contain any JARs inside, as it used to be with ant based projects.


我的目录结构应该怎么样喜欢?像src,bin,
这样的文件夹呢?

What should my directory structure look like? Folders like src, bin, anything else?

像标准的maven项目:)( http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

As in a standard maven project :) (http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html)


  • src / main / java 应用程序源(Java类)

  • src / test / java 应用程序测试(com.company.Foo
    的单元测试也应该在com.company包中,它引入一些订单
    有时使测试更容易)

  • src / main / resources 应用程序资源,例如 message.properties 文件服务国际化

  • src / test / resources 用于测试的资源,例如,如果要使用http服务器返回某些内容进行集成测试,则转储网站。

  • src/main/java application sources (Java classes)
  • src/test/java application tests (unit test for class com.company.Foo should be also in the com.company package, it introduces some order and sometimes makes testing easier)
  • src/main/resources application resources, for example message.properties file serving internationalization
  • src/test/resources resources for tests, for example dumps of websites if you would like to do integration tests with http server returning some content.

还有其他目录,例如src / main / webapp - 所有这些都在maven文档中描述。

There are also other directories, such as src/main/webapp - all of them are described in the maven docs.


我的应用程序应该如何运行?对于
实例,我的应用程序需要log4j才能运行。构建包时,Maven
可以解析此依赖关系。但是当
包被构建时,你应该手动提供你的路径log4j
(使用java -cp ...) - 如果
我想要消除这个需求的方法是什么为了方便用户提供.sh和.cmd脚本?

What is the way my application should run? For instance, my application requires log4j in order to run. Maven resolves this dependency, when building the package. But when the package is built, you should manually provide the path to you log4j (with java -cp ...) - what's the way to eliminate this requirement if I'd like to just provide .sh and .cmd scripts for user's convenience?

Maven关注所有这些事情, pom.xml 文件。如果您的程序需要一些额外的库,maven将为您下载,并放在包含您的应用程序的包中。如果例如某些依赖关系将由应用程序服务器(log4j JAR)提供,您还可以设置该依赖项将被提供,然后,它将可用于运行应用程序,单元测试,但它不会是最终的软件包中的地方。

Maven takes care about all of these things, following your configuration defined in pom.xml file. If you program requires some extra-libraries, maven will download it for you, and place in the package containing your application. If, for example some dependencies will be provided by your application server (log4j JAR), you can also set that dependency is gonna be provided and then, it will be available for running application, for unit tests, but it will not be places in the final package.

maven提供的其他伟大功能是构建配置文件。例如,您可以在日常开发模式下使用某些设置,但是在生成包装的情况下,将使用其他设置。

What is additional great feature provided by maven is build profiles. You can for example use some setting in the day-to-day development mode, but in case of building package for production, other settings will be used.

这篇关于传统的开源Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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