What are the best practices for naming ant targets

2019-04-07 12:24发布

问题:

What are the best practices for naming ant targets?

For example, what would you expect the target "test" to run? All unit tests? All functional tests? Both?

What are the standard names used for running different types of tests (unit/functional/all)? Are there standards for target names to deploy software in J2SE? in J2EE?

My project uses ant for a java project with junit, Swing applications, and J2EE applications.

回答1:

See the "Naming Conventions" section on this page : The Elements of Ant Style

The following targets are common to many builds. Always avoid changing the behavior of a well-known target name. You do not need to implement all of these in a single project.

all               Build and test everything; create a distribution, optionally install. 
clean             Delete all generated files and directories. 
deploy            Deploy the code, usually to a remote server. 
dist              Produce the distributables. 
distclean         Clean up the distribution files only. 
docs              Generate all documentation. 
init              Initialize the build: create directories, call <tstamp> and other common actions. 
install           Perform a local installation. 
javadocs          Generate the Javadoc pages. 
printerdocs       Generate printable documents. 
test              Run the unit tests. 
uninstall         Remove a local installation. 

This page also provides other good guidelines.



回答2:

For my java projects I use the names defined in the maven default lifecycle as basis. For other projects I also have used the GNU autoconf standard targets.



回答3:

There is one standard that is reasonably widely used. Any target name beginning with the - character cannot be invoked from the command line, and as such must certainly be one that is not intended to be executed directly. Sometimes, this is referred to as a hidden target.



回答4:

As long as they are understandable and consistent throughout your project(s) what you name them is up to you. Simple, short verbs are usually the norm, and targets should be broken up logically.

You can go the route that Matt B suggested, naming your test targets by type:

  • test-unit
  • test-functional
  • test-all

Or you can name by tool, if, for example, you have Junit, Selenium, and Webtest functional tests:

  • test-junit
  • test-selenium
  • test-webtest
  • test-all

If you use the description attribute for targets that you want to expose to anyone building the project, users can perform an ant -p to list available targets and pick which one they want. Helpful descriptions here are probably more important and more valuable to users than what the targets are actually named.

From the Ant Manual:

The optional description attribute can be used to provide a one-line description of this target, which is printed by the -projecthelp command-line option. Targets without such a description are deemed internal and will not be listed, unless either the -verbose or -debug option is used.



回答5:

We've made good experiences with short and concise target names that use a depend on other single tasks. AFAIK the general standard set is

  • init
  • clean
  • compile
  • dist
  • test
  • report

(can't find the link, though)

which works very well for us.

When we had to differentiate test-types, we named them "test.data" and "test.nondata" to separate the test types, each of them taken as a depend by the "test" task. Maybe you should use the "java method naming convention" as another user suggested, but IMHO that doesn't matter.

If you're running the ant script manually on your local machine (which I do to test if I have broken the build script) it comes in handy if you only have to type

ant dist
ant test report

instead of

ant compile create.distribution
ant test.data test.nondata report.junit.generate report .....


回答6:

I think this is completely a point of personal preference, but I would use

  • test - for unit tests
  • test-integration - for integration tests
  • dbtest - for database tests (if they are included in the above item)
  • test-all to run all of the above

Also I'm agnostic on using test or the plural form tests, I've probably done both on different projects.



回答7:

Your naming convention for ant targets should be very similar to the naming convention for Java methods: namely, simple and descriptive of what the target does.

Here's an excerpt from the Sun standard for method naming:

Naming a Method

Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:

run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty

Probably the biggest difference is the style for ant targets, which should be all lower cased letters with words separated by dashes.

For example, the following targets seem appropriate to me:

  • run-all-tests
  • build
  • clean
  • test-and-release
  • deploy
  • run-code-coverage-metrics

In the end, try to use the same good judgment you use when naming methods. If your targets are clear, descriptive, and easy to understand, you are in good shape.

For more detailed information on the topic, check out The Elements of Ant Style on the Ant wiki.