Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Code generator maven plug-in source code how to write

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the code generator maven plug-in source code how to write, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Why write a code generator?

When writing a back-end project, the basic function is to add, delete, query and change, which is usually based on the table or the specified data model.

In most Web projects, it usually requires a lot of new basic classes to write an addition, deletion, query and modification.

Assuming that your project is made up of control layer, service layer, and data access layer, then if you add a table add, delete, query and modify function, we may need to create the following classes for this table.

Model.javaModelController.javaIModelService.javaModelService.javaIModelDao.javaModelDao.java

After creation, some project structures have common basic classes, and you may need to add generics based on the Model type, which requires another copy-and-paste operation.

Interface IModelService extends IBaseService {} class ModelService extends BaseService implements IModelService {} interface IModelDao extends IBaseDao {} class ModelDao extends BaseDao implements IModelDao {}

Of course, the project structure of each company is different, the above operations may be more or less, but for a background system, a large number of similar additions, deletions and modifications are necessary. If your project structure is irregular, then the code generator may not be right for you

Level of code generator

Copy and paste code generator

As the name implies, the simplest and most frequently used way is to directly make a copy of other similar existing documents and change the key information. The cost is the lowest and the time-consuming is determined according to the files copied by the project. It is possible to cause code exceptions due to the lack of certain changes.

Generally speaking, the code is generated by copy and paste, which is easy to use and has no learning cost, but it is easy to make mistakes, there are certain modification costs, and the time-consuming is proportional to the number of files and modified content.

The framework comes with a code generator

Usually bound to the framework, it is difficult to implant existing systems and has high limitations. The format of the generated code is relatively simple, so it is difficult to use it flexibly.

Custom Code Generator

Because the code structure of most systems is relatively unified, but some different, so in the market there is no suitable for their own generator, we will choose to write one, through the direct implementation of the main method to generate the file, define a few parameters, in the need to generate when modified execution.

The code generator introduced in this article is mainly extended on this basis, through the way of the maven plug-in, the parameters that need to be customized will be run through the plug-in configuration, such a set of code is used frequently, only maven dependencies need to be added, and the code of the generator does not need to be copied many times and is difficult to maintain.

Custom Code Generator maven plug-in

The principle of the code generator is roughly based on the specified template, pass in dynamic parameters, replace the template content, and dynamically generate different file code.

Through the way of maven plug-in, can be well integrated into our project, just add plug-in dependencies, and then add a few configurations, execute maven-related commands, you can complete the code generation operation.

This is a code generator that I wrote in order to provide a better development experience for my team members.

Address: https://github.com/k55k32/simple-codegen

Simple-codegen uses freemarker as the template engine, combined with the maven plug-in to simplify the parameter configuration, and multi-project use only need to add plug-in dependencies, and modify the parameters, customize the template, you can generate code with one click.

Because the template can be customized according to the project, in almost all cases, you only need to define the template and configure the parameters to start using.

The current parameters are fixed, and some dynamic parameters may be supported in the future, such as:

Read table structure as a parameter

Call the API directly to get the return value as a parameter

Call the local code directly to get the return value as a parameter

Explain the details of code implementation

The code is really simple. At present, there is only one template rendering function. You can look at the source code by yourself. If you don't understand, you can mention Issue, and if you don't understand, you can mention Pull Request.

Upload to Central Warehouse tutorial

I am really writing tutorials while uploading, real hands-on experience.

1. Apply for a central warehouse account

Enter https://issues.sonatype.org

Click Sign up to create an account

Create a new one with a question type of New Project, and note that Group Id should be the same as your own project pom.xml. If it is your own domain name, you need to have domain name DNS management permission, because the warehouse administrator will require you to add a TXT record to the specified address. If you don't have your own domain name, you can use your own Github address, and the administrator will ask you to create a warehouse with a specified name.

After submitting the application, the administrator will inform you that the user can upload the snapshot version or release version.

The previous registration work is completed, and then there is the upload process, and the username password used in the next process uses the username password just created.

2. Pom.xml modification and settings.xml configuration

Add your own account password in the local maven configuration settings.xml (required for upload), and id can be customized. The account password can be entered in clear text, or you can log in to https://oss.sonatype.org and click the user name in the upper right corner to enter Profile, and then select User Token to create an encrypted Access User Token, which can replace the account password in the settings.xml. This can avoid saving the user name and password in plaintext.

Sonatype-center youRegisterUsername youRegisterPassword

Add the address of the remote release repository in pom.xml. Note that id needs to be consistent with settings-xml configuration.

Sonatype-center releases repo https://oss.sonatype.org/service/local/staging/deploy/maven2 sonatype-center snapshots repo https://oss.sonatype.org/content/repositories/snapshots

If you want to standardize the content of pom.xml file, you need to add the following tag description. The content is customized. Please ignore it if it has been added. Please refer to pom.xml

Simple-codegen https://diamondfsd.com simple code generate maven plugin. Template rendering through Java and freemarker. Diamondfsd Diamond Zhou diamondfsd@gmail.com https://diamondfsd.com 8 The MIT License https://opensource.org/licenses/MIT repo https://github.com/k55k32/simple-codegen.git 3. File signature (cannot be published without signature)

Signature tutorial

Download the windows version key generation tool: https://gpg4win.org/download.html

The name after the installation is: after the Kleopatra is opened, create a new key pair, and after the creation is complete.

Note that if you are running maven in an environment such as IDE, you need to restart IDE after installing Kleopatra, otherwise the environment variable does not take effect, and the gpg command cannot be found when executing the maven plug-in

Pom.xml add plug-in configuration

Org.apache.maven.plugins maven-gpg-plugin 1.5 sign-artifacts verify sign 4. Release to the central warehouse

Perform mvn clean deploy upload to the central warehouse

Log in to https://oss.sonatype.org/ and click the Staging Repositoryies menu on the left, then drop down to the bottom to find the package you just uploaded

Select the change package and click Close

After the Close is completed, click Release (if the Close fails, click on the project, and you can see the cause of the failure in the bottom Activity and solve it according to the reason)

After the Release is completed, your package will enter the central repository synchronization queue. After the synchronization is completed, others can directly add your package dependencies in the pom.xml. The specific synchronization time is officially given that it will be synchronized to the central repository within 10 minutes. You can refer to the dependency. Then synchronize to the search engine within 2 hours, which can be found in https://search.maven.org.

The above is the code generator maven plug-in source code how to write, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report