您在 Java 中为个人/业余项目使用什么包命名约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/292169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:39:47  来源:igfitidea点击:

What package naming convention do you use for personal/hobby projects in Java?

javanaming-conventions

提问by Mike Spross

I'm already familiar with the standard Java package naming convention of using a domain name to create a unique package name (i.e. package com.stackoverflow.widgets). However, I've never seen any recommendations for how to choose package names for personal projects. I assume because this is because this is really a matter of personal taste.

我已经熟悉使用域名创建唯一包名称(即 package com.stackoverflow.widgets)的标准 Java 包命名约定。但是,我从未见过有关如何为个人项目选择包名称的任何建议。我想是因为这是因为这真的是个人品味的问题。

So, how do you choose package names for personal projects that will never make it into production (you might be experimenting with a new framework in your spare time). Assuming you don't have a personal website whose domain you can use to create your package structure, what do (or would) you do? Do you have a logical system in place for generating new package names for hobby projects, or do you just use simple throw-away package names like mypackage?

那么,您如何为永远不会投入生产的个人项目选择包名称(您可能在业余时间尝试新框架)。假设您没有可以使用其域来创建包结构的个人网站,您会(或将)做什么?你是否有一个逻辑系统来为业余项目生成新的包名,或者你只是使用简单的一次性包名,比如mypackage

Since I'm just curious to see what different people's thoughts are on this, I've made this a community wiki.

由于我只是想知道不同的人对此有何看法,因此我将其设为社区维基。

For me personally, I've never given it much thought, but I wanted to play around with Wickettonight and it occurred to me that I don't have a clear idea of how I want to organize my hobby projects. A separate, distinct package naming convention for hobby projects (in my mind, at least) would serve as a good way to keep personal and work-related code clearly separate from each other.

就我个人而言,我从来没有想过它,但我今晚想和Wicket一起玩,我突然想到我对如何组织我的爱好项目没有明确的想法。一个单独的、独特的爱好项目的包命名约定(至少在我看来)将作为一个很好的方式来保持个人和工作相关的代码彼此清晰地分开。

I was thinking of a simple hierarchal naming convention, to keep the source for my personal projects in a single root folder:

我正在考虑一个简单的分层命名约定,将我的个人项目的源代码保存在一个根文件夹中:

  • Use myprojectsas the root folder
  • Append the project name
  • Add any additional subpackage names
  • 使用myprojects作为根文件夹
  • 附加项目名称
  • 添加任何其他子包名称

So, my Wicket project would be in the package myprojects.learningwicketand unit tests would be in the package myprojects.learningwicket.tests(for example).

因此,我的 Wicket 项目将在包中myprojects.learningwicket,单元测试将在包中myprojects.learningwicket.tests(例如)。

采纳答案by Greg Hewgill

If you're just doing personal projects where nobody else will use the code, then you can make up a package name that you like. Don't make up something that starts with com.or net.or other top-level domain though, because that would imply that you own the domain name (ie. using com.johnas your package name just because your name happens to be John is not a good idea).

如果您只是在做其他人不会使用代码的个人项目,那么您可以创建一个您喜欢的包名称。不要编造以com.ornet.或其他顶级域开头的内容,因为这意味着您拥有该域名(即,com.john仅因为您的名字恰好是 John 而用作您的包名称并不是一个好主意) .

If you're going to give the code to anybody else, you should use a globally unique package name, which according to Java conventions means you should register and use a domain name.

如果您要将代码提供给其他任何人,则应使用全局唯一的包名称,根据 Java 约定,这意味着您应该注册并使用域名。

回答by Don Kirkby

I store most of my hobby projects in Google Code, so I just use the project site as the package name: com.googlecode.donkirkby.someproject.

我将大部分爱好项目存储在 Google Code 中,因此我仅使用项目站点作为包名称:com.googlecode.donkirkby.someproject.

回答by anjanb

my name is anjan

我的名字是anjan

usually, I use com.anjan

通常,我使用 com.anjan

I have a fantasy company of my own -- sometimes I use that

我有自己的幻想公司——有时我会使用它

the tradition with sourceforge(as hibernate and other packages showed) is net.sf.*

sourceforge 的传统(如 hibernate 和其他软件包所示)是 net.sf.*

so, depending on your mood, you can go with that.

所以,根据你的心情,你可以去。

回答by Florin

I just use my initials: fg.nameofproject.etc

我只使用我的姓名首字母:fg.nameofproject.etc

It reduces typing. It can be prefixed at any time with sf.net or com. or org. or com.google..

它减少了打字。它可以随时以 sf.net 或 com 为前缀。或组织。或 com.google..

As the project is personal treat it special just like your freshly pressed personalized gift shirt - it will feel good.

由于该项目是个性化的,就像您刚压过的个性化礼品衬衫一样特别对待它 - 它会让人感觉很好。

回答by Jim Nelson

I think you've got it worked out. The temptation to avoid here is to not bother with a package name at all. It's easy to save a few keystrokes because "I'm just writing some test code." But then the code gets good and useful and large, and then you realize you have a solid start for what may be a long-lived library or application. It may not be a library or an application that ever leaves your home network, but the point is, you've not thought ahead. That's the Denmark ghost of computer science -- always think ahead, if only a little bit.

我想你已经解决了。这里要避免的诱惑是根本不打扰包名称。保存几次击键很容易,因为“我只是在编写一些测试代码”。但是随后代码变得很好、有用并且很大,然后你意识到你有一个可能是长期存在的库或应用程序的坚实的开端。它可能不是离开您家庭网络的图书馆或应用程序,但关键是,您没有提前考虑过。这就是丹麦计算机科学的幽灵——总是提前考虑,哪怕只是一点点。

The naming convention I use for my hobby code is very much like yours. I have a top-level directory named "futura" (long, boring reasons why that name came about) that all my code hangs off of. I do try and organize my code into package libraries, even if it might be a class or package I never use for another project. I place all applications (that is, anything that has a void main(String[] args) in the class) in the futura.app.* folder. I also try to mimic the standard Java library package names for my own code, although in a couple of cases I broke convention because of my own tastes (i.e. futura.inet for Internet, not merely socket, code, and futura.collections for non-util stuff.) To paraphrase David Mamet: Always be genericizing. Always be genericizing!

我用于我的爱好代码的命名约定与您的非常相似。我有一个名为“futura”的顶级目录(这个名字出现的漫长而无聊的原因),我的所有代码都挂在它上面。我确实尝试将我的代码组织到包库中,即使它可能是我从未用于其他项目的类或包。我将所有应用程序(即类中任何具有 void main(String[] args) 的应用程序)放在 futura.app.* 文件夹中。我也尝试为我自己的代码模仿标准的 Java 库包名称,尽管在一些情况下我因为自己的喜好打破了惯例(即 Internet 的 futura.inet,而不仅仅是 socket、code 和 futura.collections 的非-util 的东西。)套用 David Mamet 的话:总是泛化。 总是泛化!

From the care you used to post the question, I suspect you agree with my last point as well: You don't have to treat hobbyist hacking as an enterprise-level project, but if you bring some of that discipline to the home game, the hobby is all the more rewarding.

从您过去发布问题的谨慎来看,我怀疑您也同意我的最后一点:您不必将业余黑客视为企业级项目,但是如果您将某些纪律带入家庭游戏,爱好更有意义。

回答by Lawrence Dol

I just use my name: surname.initials.xxx, as a nice comprise between brevity and collision avoidance. I figured that would give a reasonable collision free namespace should I ever choose to publicly publish the code. I also have a little program I've written which can repackage entire directory trees so I figured should I ever need to repackage for publishing it's quite painless... therefore I didn't loose too much sleep over it.

我只是使用我的名字:surname.initials.xxx,作为简洁和避免碰撞之间的一个很好的组合。我认为如果我选择公开发布代码,这将提供一个合理的无冲突命名空间。我还有一个我写的小程序,它可以重新打包整个目录树,所以我想我是否需要重新打包发布它是非常轻松的......因此我并没有因为它而睡太多觉。

After the surname.initials.xxx, I use either app for application packages, lib for library packages, and tst for stuff I am purely experimenting with.

在 surname.initials.xxx 之后,我将 app 用于应用程序包,将 lib 用于库包,将 tst 用于我纯粹试验的东西。

回答by James Schek

<sarcasm>Bah. Any self-respecting programmer would have his or her own domain name. This is clearly a trick question. Everyone has their own personal domain name! </sarcasm>:-)

<sarcasm>呸。任何有自尊的程序员都会拥有自己的域名。这显然是一个棘手的问题。每个人都有自己的个人域名!</sarcasm>:-)

Ok, in all seriousness, buying a custom domain name is probably the easiest option. For about $10 a year, you can find reputable providers to host the domain and forward email.

好吧,严肃地说,购买自定义域名可能是最简单的选择。每年大约 10 美元,您就可以找到信誉良好的提供商来托管域和转发电子邮件。

回答by James Schek

what you think about lastname.firstname.project ??? like luz.marlon.project ?

你怎么看 lastname.firstname.project ???像 luz.marlon.project ?

回答by tehvan

I thought about asking this same question. So far i've used the prefix com.tehvan, even though i don't actually have a company.

我想过问同样的问题。到目前为止,我使用了前缀 com.tehvan,即使我实际上没有公司。

回答by paulmurray

pmurray_at_bigpond_dot_com.project.package