Open source projects often see package-info.java files. What’s the use?

Time:2021-11-29

Source:https://www.toutiao.com/i6992877750461825544/

1、 Introduction to packge-info.java

Packge-info.java is a java file that can be added to any Java source package. The goal of packge-info.java is to provide a package level document description or package level comments.

In the packge-info.java file, the only content required to be included is the package declaration statement, such as:

/**
* This is domain module, the core business logic is implemented here.
*
*/
package com.rickie.tracking.domain;

Before Java 5, the package level document was package.html, which was generated through Javadoc. In Java version 5 and above, the package description and related documents can be written into the packge-info.java file, which is also used for Javadoc generation.

2、 Generate Javadoc documents using idea

In idea, select tools / generate Javadoc… Menu item, as shown in the figure:

In the generate Javadoc window, enter the relevant settings.

Step 1: select the scope of generating Javadoc documents. In the figure below, we can select module.

Step 2: it is better to create a new folder for the output directory, which is more organized and tidy.

Step 3: set the locale to determine the language of the document. Simplified Chinese is zh_ Cn, traditional (Taiwan) zh_ TW, traditional (Hong Kong) zh HK, English (Hong Kong) en HK, English (US) en US, English (UK) en GB, English (global) en ww.

After setting, click [OK] to generate doc document. Find the output directory and open index.html to see the generated document.

View the document of the class, as shown in the figure:

3、 Create package info file

The package info file cannot be created casually. An error “this is not a valid Java qualified name” will be reported. The class name is invalid. The Java variable definition specification is: letters, numbers, underscores, and the less commonly used $symbol. The middle dash is not included. How to create this file?

You can create a normal file, as shown in the figure. The file name is package-info.java. Or a more direct way is to copy one from another project.

4、 Provide package level variables

If you want to use the corresponding variables in the package instead of other packages, you can put the variables under package-info.java to realize the concept of subcontracting for your own use.

/**
* This is domain module, the core business logic is implemented here.
*
*/
package com.rickie.tracking.domain;

/**
*Package constant
*/
class PACKAGE_CONST{
 Public static final string Cola = "Introduction to cola 4. X architecture and project practice" The technical column first introduces the introduction to the use of cola framework and the integration with idea development tools; then, based on the cola architecture, it creates a classic DDD example project - freight transportation system, and introduces in detail DDD domain modeling, code development of adaptation layer, application layer, domain layer and infrastructure layer, design and implementation of anti-corrosion layer (ACL), domain event Introduction practice, and message publishing and subscription based on Kafka message middleware. The content goes from simple to deep, starting from the development practice, and gradually master the idea of building complex business application systems based on Cola architecture and DDD domain modeling. ";
}

Call the package constant in any class in the package. The example code is as follows:

public class Test {
 public static void main(String[] args) {
 System.out.println(PACKAGE_CONST.COLA);
 }
}

When calling package constants from any class outside the package, you will be prompted with an error.

5、 Provide package level comments

When using Javadoc, add comments in package-info.java to generate the notes of the corresponding packages implemented by Javadoc.

/**
*Description: Cola domain module
* This is domain module, the core business logic is implemented here. 
*Creation time: 12:00, August 1, 2021 
* @version v1.0
*/
package com.rickie.tracking.domain;

/**
*Package constant
*/
class PACKAGE_CONST{
 Public static final string Cola = "Introduction to cola 4. X architecture and project practice" The technical column first introduces the introduction to the use of cola framework and the integration with idea development tools; then, based on the cola architecture, it creates a classic DDD example project - freight transportation system, and introduces in detail DDD domain modeling, code development of adaptation layer, application layer, domain layer and infrastructure layer, design and implementation of anti-corrosion layer (ACL), domain event Introduction practice, and message publishing and subscription based on Kafka message middleware. The content goes from simple to deep, starting from the development practice, and gradually master the idea of building complex business application systems based on Cola architecture and DDD domain modeling. ";
}

Generate the corresponding Javadoc document, as shown in the figure:

Recent hot article recommendations:

1.1000 + java interview questions and answers (2021 latest version)

2.Stop playing if / else on the full screen. Try the strategy mode. It’s really fragrant!!

3.what the fuck! What is the new syntax of XX ≠ null in Java?

4.Spring boot 2.5 heavy release, dark mode is too explosive!

5.Java development manual (Songshan version) is the latest release. Download it quickly!

Feel good, don’t forget to like + forward!