Monday 25 May 2020

Spring boot Create a Deployable War File

It is easy to follow https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

But here I want to add one thing is how to access the application context?

One way is you can get the context from below method return:

ConfigurableApplicationContext applicationContext = app.run(args);

Another is if you deploy the .war to external Tomcat server, the applicationContext may be null, in this case, you can get it from onStartup() method

@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        // set context into your own class, say MySpringContext here
        MySpringContext.setContext(rootAppContext);
        servletContext.addListener(new ContextLoaderListener(rootAppContext) {
            @Override            public void contextInitialized(ServletContextEvent event) {
                // no-op because the application context is already initialized            }
        });
    }
    else {
        this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not "                + "return an application context");
    }
}

Saturday 23 May 2020

Create multiple yml files in Azure pipeline

If you want to have multiple pipeline yml files other than the single default one (with name: azure-pipelines.yml):

Step 1:
Click "pipeline" menu in Azure DevOps, and create a new pipeline yml file. You can rename the yml file with a meanful name, after modify the content, create Save button on the top-right, normally you will choose create a new branch to hold your new created yml file. If this yml is not the default one to be triggered by "- master", you can set
trigger:
none

Step 2:
Run the pipeline to see if it works as your expectation. If it is, you can merge the yml file into "master" branch

Step 3: You can manually run the new created pipeline yml file as per needs.

Normally we only have one yml file with trigger "- master", the other yml files can be triggered manually or per scheduled. As the yml file will be merged into master branch, so it can be ran in master branch, and any branches from master afterwards.