Creating a module
A JHipster module is a Yeoman generator that is composed with a specific JHipster sub-generator to inherit some of the common functionality from JHipster. A JHipster module can also register itself to act as a hook from the JHipster generator.
JHipster modules are listed on the JHipster marketplace.
This allows to create third-party generators that have access to the JHipster variables and functions, and act like standard JHipster sub-generators. The hook mechanism invokes third-party generators before and after app generation and entity generation.
Example
The JHipster Fortune module generates a “fortune cookie” page in a JHipster-generated application.
It is our sample module that showcases how you can use JHipster’s variables and functions in order to create your own generator.
Or, you can use the JHipster module generator to help you to initialize your module.
Basic rules for a JHipster module
A JHipster module:
- is an NPM package, and is a Yeoman generator.
- follows an extension of the Yeoman rules listed at http://yeoman.io/generators/ and can be installed, used and updated using the “yo” command. Instead of being prefixed by “generator-“, it is prefixed by “generator-jhipster-“, and instead of having just the “yeoman-generator” keyword, it has 2 keywords, “yeoman-generator” and “jhipster-module”.
- A JHipster module registering as a hook should not call
process.exit
in its generators being hooked.
Composability
A JHipster module uses the new “composability” feature from Yeoman, described at http://yeoman.io/authoring/composability.html to have access to JHipster’s variables and functions.
For this, it composes with the “jhipster:modules” sub generator:
compose: function() {
this.composeWith('jhipster:modules', {
options: {
jhipsterVar: jhipsterVar,
jhipsterFunc: jhipsterFunc
}
});
},
Hooks
JHipster will call certain hooks before and after some of its tasks, currently available and planned tasks are listed below.
- Post Entity creation hook
- Pre Entity creation hook [planned]
- Post App creation hook [planned]
- Pre App creation hook [planned]
A JHipster module can register to act as a hook when its main generator is run by the end user. You need to call the registerModule
method available in jhipsterFunc
from your main (app) generator to register as hook, you need to pass the below parameters in the method as below
jhipsterFunc.registerModule(npmPackageName, hookFor, hookType[, callbackSubGenerator[, description]])
npmPackageName
npm package name of the generator. e.g:jhipster-generator-fortune
hookFor
which Jhipster hook from above this should be registered to ( values must beentity
orapp
)hookType
where to hook this at the generator stage ( values must bepre
orpost
)callbackSubGenerator
[optional] sub generator to invoke, if this is not given the module’s main (app) generator will be called, e.g:bar
orfoo
generatordescription
[optional] description of the generator, if this is not given we will generate a default based on the npm name given
Variables available
Global variables:
baseName
: the name of the applicationpackageName
: the Java package nameangularAppName
: the AngularJS application namejavaDir
: the directory for the Java application, including the package foldersresourceDir
: the directory containing the Java resources (alwayssrc/main/resources
)webappDir
: the directory containing the Web application (alwayssrc/main/webapp
)
And all the variables from the JHipster .yo-rc.json
file:
authenticationType
: the type of authenticationhibernateCache
: the Hibernate 2nd level cacheclusteredHttpSession
: whether a clustered HTTP session is usedwebsocket
: whether WebSockets are useddatabaseType
: the type of database useddevDatabaseType
: the database used in “dev” modeprodDatabaseType
: the database used in “prod” modesearchEngine
: whether a search engine is useduseSass
: if Sass is used for CSS pre-processingbuildTool
: the Java build toolfrontendBuilder
: the front-end (JavaScript/CSS/HTML) build toolenableTranslation
: if translations are enabledenableSocialSignIn
: if social login is enabledtestFrameworks
: an array of the test frameworks selected
Functions available
addJavaScriptToIndex
: add a JavaScript file to theindex.html
addMessageformatLocaleToIndex
: add a message format locale (for i18n)addElementToMenu
: add an entry in the navigation menuaddEntityToMenu
: add an entity in the entity navigation sub-menuaddElementToAdminMenu
: add an entry in the admin navigation sub-menuaddElementTranslationKey
: add a new translation key in theglobal.json
fileaddEntityTranslationKey
: add a new translation key for an entity in theglobal.json
fileaddAdminElementTranslationKey
: add a new translation key for an admin sub-menu in theglobal.json
fileaddGlobalTranslationKey
: add a new translation key in theglobal.json
fileaddTranslationKeyToAllLanguages
: add a new translation key for all installed languages using methodsaddElementTranslationKey
,addEntityTranslationKey
,addAdminElementTranslationKey
getAllSupportedLanguages
: get the list of languages supported by JhipstergetAllInstalledLanguages
: get the list of languages installed by current applicationaddChangelogToLiquibase
: add a new changelog in the Liquibasemaster.xml
fileaddColumnToLiquibaseEntityChangeset
: add new columns to the Liquibase changelog for an entitydateFormatForLiquibase
: creates a new timestamp to be used by a Liquibase changelogcopyI18nFilesByName
: copy i18n filesaddMavenDependency
: add a new maven dependency in thepom.xml
fileaddMavenPlugin
: add a new maven plugin in thepom.xml
fileaddGradleDependency
: add a new gradle dependencyaddGradlePlugin
: add a new gradle pluginapplyFromGradleScript
: apply script from another gradle fileaddBowerDependency
: add a new package in thebower.json
fileaddBowerOverride
: add an override configuration in thebower.json
fileaddAngularJsModule
: add a new module in theapp.js
fileaddAngularJsConfig
: add a new config in theapp.js
fileaddAngularJsInterceptor
: register an angular js interceptor in theapp.js
fileaddMainCSSStyle
: add a new style in themain.css
fileaddMainSCSSStyle
: add a new style in themain.scss
filecopyTemplate
: copy a template from source to a destination after stripping any translation content when translation is disabledcopyHtml
: short hand method forcopyTemplate
which is defaulted to actionstripHtml
copyJs
: short hand method forcopyTemplate
which is defaulted to actionstripJs
rewriteFile
: add the given content above a specific custom needle in a filereplaceContent
: replace the given content for a specific pattern/regex in a fileregisterModule
: register to act as a hook from app or entity generatorupdateEntityConfig
: update the json configuration file for an entity with given key and valuegetModuleHooks
: get the array of all registered hooks for the application
Registering a module to the JHipster marketplace
To have your module available in the JHipster marketplace, you need to add it to the modules.json file by doing a Pull Request to the jhipster/jhipster.github.io project.
The modules.json
is a JSON file containing an array of the available modules. Add a new module in the array, and specify all fields. Leave the “verified” field as false: your module will become “verified” if the JHipster team verifies it. Specify the JHipster version required for your module to work by adding the jhiVersionRequired
property, follow semver operators to define a range.
Once your Pull Request is accepted, your module will become available in our marketplace.