'Deploy Jar with external properties file spring boot

I have a problem with external properties file used by jar file generated by spring boot I have created the project below :

  1. The model object :

    package jms.model;
    public class EventHandlerMessage {
    
    private long idDemande;
    private String typeEvent;
    private String dateEvent;
    private String typeProcessus;   
    private String businessDataType;
    
    public long getIdDemande() {
        return idDemande;
    }
    public void setIdDemande(long idDemande) {
        this.idDemande = idDemande;
    }
    public String getTypeEvent() {
        return typeEvent;
    }
    public void setTypeEvent(String typeEvent) {
        this.typeEvent = typeEvent;
    }
    public String getDateEvent() {
        return dateEvent;
    }
    public void setDateEvent(String dateEvent) {
        this.dateEvent = dateEvent;
    }
    public String getTypeProcessus() {
        return typeProcessus;
    }
    public void setTypeProcessus(String typeProcessus) {
        this.typeProcessus = typeProcessus;
    }
    public String getBusinessDataType() {
        return businessDataType;
    }
    public void setBusinessDataType(String businessDataType) {
        this.businessDataType = businessDataType;
    }
    

    }

  2. Configuration class :

    package jms.config;
    
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
    import org.springframework.stereotype.Component;
    
    @Component
    @PropertySource("classpath:application.properties")
    @ConfigurationProperties("jms")
    public class JmsConfig {    
    private String server_url;
    
    private String user_name;
    
    private String password;
    
    private String default_destination_name;
    
    private String reject_destination_name;
    
    private String error_destination_name;
    
    public String getError_destination_name() {
        return error_destination_name;
    }
    
    public void setError_destination_name(String error_destination_name) {
        this.error_destination_name = error_destination_name;
    }
    
    public String getReject_destination_name() {
        return reject_destination_name;
    }
    
    public void setReject_destination_name(String reject_destination_name) {
        this.reject_destination_name = reject_destination_name;
    }
    
    public String getDefault_destination_name() {
        return default_destination_name;
    }
    
    public void setDefault_destination_name(String default_destination_name) {
        this.default_destination_name = default_destination_name;
    }
    
    public String getServer_url() {
        return server_url;
    }
    
    public void setServer_url(String server_url) {
        this.server_url = server_url;
    }
    
    public String getUser_name() {
        return user_name;
    }
    
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    @Override
    public String toString() {
        return "JmsConfig [server_url=" + server_url + ", user_name=" + user_name + ", password=" + password + "]";
    }
    
     @Bean
        public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    

    }

  3. Service

    package jms.service;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.MessageProducer;
    import javax.jms.Queue;
    import javax.jms.QueueConnection;
    import javax.jms.QueueReceiver;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import fr.harmonieMutuelle.bpm.lib.jms.model.EventHandlerMessage;
    
    public class JmsSenderService {
    
    static Logger LOGGER = Logger.getLogger("jms.service.JMSExample");
    
    private String serverUrl;
    
    private String userName;
    
    private String password;
    
    
    public String getServerUrl() {
       return serverUrl;
    }
    
    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }
    
    public String getUserName() {
        return userName;
    }
    
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    static QueueConnection connection;
    static QueueReceiver queueReceiver;
    static Queue queue;
    
    static TextMessage message;
    
    public void sendTopicMessage(String topicName, EventHandlerMessage message) {
    
    Connection connection = null;
    Session session = null;
    MessageProducer msgProducer = null;
    Destination destination = null;
    
    
    try {
        TextMessage msg;
    
        LOGGER.info("Publishing to destination '" + topicName + "'\n");
    
        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
                serverUrl);
    
        connection = factory.createConnection(userName, password);
    
    
        session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
    
    
    
        destination = session.createQueue(topicName);
    
    
        msgProducer = session.createProducer(null);
    
    
    
        msg = session.createTextMessage();
    
        ObjectMapper objestMapper = new ObjectMapper();  
        String jsonMessage = objestMapper.writeValueAsString(message);  
        msg.setText(jsonMessage);
    
        msgProducer.send(destination, msg);
    
    
    
        LOGGER.info("Published message: " + jsonMessage);
    
    
        connection.close();
    
    } catch (Exception e) {
        e.printStackTrace();
    }
     }
     }
    
  4. Springboot application

    package lib.main;
    
    import java.time.LocalDateTime;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import jms.config.JmsConfig;
    import jms.model.EventHandlerMessage;
    import jms.service.JmsSenderService;
    
    @SpringBootApplication(scanBasePackages={
     "lib.main", "jms.config"})
    public class DemoApplication implements CommandLineRunner {
    
    @Autowired
    private JmsConfig jmsConfig;
    
    @Override
    public void run(String... args) {
    System.out.println(jmsConfig);
    JmsSenderService sender = new JmsSenderService();
    EventHandlerMessage eventHandlerMessage = new EventHandlerMessage();
    eventHandlerMessage.setDateEvent(LocalDateTime.now().toString());
    eventHandlerMessage.setIdDemande(12006);
    eventHandlerMessage.setTypeEvent("PROCESSINSTANCE_STATE_UPDATED");
    eventHandlerMessage.setBusinessDataType("");
    eventHandlerMessage.setTypeProcessus("");
    
    sender.setServerUrl(jmsConfig.getServer_url());
    sender.setUserName(jmsConfig.getUser_name());
    sender.setPassword(jmsConfig.getPassword());
    
    sender.sendTopicMessage(jmsConfig.getDefault_destination_name(),eventHandlerMessage);
    }
    
    public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
     }
    } 
    
  5. pom.xml :

          <project xmlns="http://maven.apache.org/POM/4.0.0" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
             https://maven.apache.org/xsd/maven-4.0.0.xsd">     
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.myproject</groupId>    
    <artifactId>event-handler-app</artifactId>  
    <packaging>jar</packaging>
    
    <properties>        
        <bonita.version>7.12.1</bonita.version>          
        <com.tibco.tibjms.version>8.1.0</com.tibco.tibjms.version>      
        <javax.jms-api.version>2.0.1</javax.jms-api.version>         
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>       
        <jackson.version>2.5.2</jackson.version>         
        <springboot.version>2.4.0</springboot.version>      
        <spring-jms.version>4.3.3.RELEASE</spring-jms.version>  
    </properties>   
    <dependencies>      
      <dependency>          
         <groupId>org.bonitasoft.engine</groupId>           
         <artifactId>bonita-server</artifactId>             
         <version>${bonita.version}</version>       
      </dependency>         
      <dependency>          
         <groupId>com.tibco</groupId>           
         <artifactId>tibjms</artifactId>            
         <version>${com.tibco.tibjms.version}</version>         
      </dependency>         
      <dependency>          
         <groupId>javax.jms</groupId>           
         <artifactId>javax.jms-api</artifactId>             
         <version>${javax.jms-api.version}</version>        
      </dependency>         
      <dependency>          
         <groupId>org.bonitasoft.engine</groupId>           
         <artifactId>bonita-client</artifactId>             
         <version>${bonita.version}</version>       
      </dependency>         
      <dependency>          
         <groupId>com.fasterxml.jackson.core</groupId>          
         <artifactId>jackson-databind</artifactId>          
         <version>${jackson.version}</version>      
      </dependency>         
      <dependency>          
         <groupId>com.fasterxml.jackson.core</groupId>          
         <artifactId>jackson-core</artifactId>          
         <version>${jackson.version}</version>      
      </dependency>         
      <dependency>          
         <groupId>com.fasterxml.jackson.core</groupId>          
         <artifactId>jackson-annotations</artifactId>           
         <version>${jackson.version}</version>      
      </dependency>         
      <dependency>          
         <groupId>org.springframework</groupId>             
         <artifactId>spring-jms</artifactId>            
         <version>${spring-jms.version}</version>       
      </dependency>         
      <dependency>          
         <groupId>org.springframework.boot</groupId>            
         <artifactId>spring-boot-starter</artifactId>           
         <version>${springboot.version}</version>       
      </dependency>         
      <dependency>          
         <groupId>org.springframework.boot</groupId>            
         <artifactId>spring-boot-starter-test</artifactId>           
         <version>${springboot.version}</version>           
         <scope>test</scope>        
      </dependency>         
      <dependency>          
         <groupId>org.springframework.boot</groupId>            
         <artifactId>spring-boot-configuration-processor</artifactId>            
         <version>${springboot.version}</version>           
         <optional>true</optional>      
      </dependency>         
      <dependency>          
         <groupId>junit</groupId>           
         <artifactId>junit</artifactId>             
         <version>4.11</version>        
      </dependency>
      <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>1.3.156</version>
      </dependency>         
    </dependencies>
    
    
    <profiles>
     <profile>          
        <id>dev</id>            
        <activation>
                <activeByDefault>true</activeByDefault>             
        </activation>           
        <properties>
                <envClassifier>dev</envClassifier>          
        </properties>           
        <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>3.0.0</version>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/configuration.xml</descriptor>
                            </descriptors>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${springboot.version}</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.22.0</version><!--$NO-MVN-MAN-VER$ -->
                    </plugin>
                </plugins>          
       </build>
    </profile>      
    <profile>           
      <id>int</id>          
      <properties>
                <envClassifier>int</envClassifier>          
      </properties>             
      <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>3.0.0</version>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/configuration.xml</descriptor>
                            </descriptors>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${springboot.version}</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.22.0</version><!--$NO-MVN-MAN-VER$ -->
                    </plugin>
                </plugins>          
         </build>       
     </profile>             
     <profile>          
      <id>liv</id>          
      <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>3.0.0</version>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/configuration_tar.xml</descriptor>
                            </descriptors>
                            <appendAssemblyId>true</appendAssemblyId>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly2</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>                            
                            <execution>
                                <id>make-assembly1</id>
                                <phase>install</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>                                
                            </execution>
                        </executions>
                    </plugin>
                </plugins>          
         </build>       
    </profile>  
    
  6. HandlerEvent object :

       package lib.events;
    
       import java.util.List;
       import java.util.UUID;
    
       import org.bonitasoft.engine.api.APIClient;
       import org.bonitasoft.engine.api.ProcessAPI;
       import org.bonitasoft.engine.bpm.data.DataInstance;
       import org.bonitasoft.engine.bpm.data.DataNotFoundException;
       import org.bonitasoft.engine.core.process.definition.model.SFlowNodeType;
       import org.bonitasoft.engine.core.process.instance.api.ActivityInstanceService;
       import org.bonitasoft.engine.core.process.instance.model.SFlowNodeInstance;
       import org.bonitasoft.engine.core.process.instance.model.SHumanTaskInstance;
       import org.bonitasoft.engine.events.model.SEvent;
       import org.bonitasoft.engine.events.model.SHandler;
       import org.bonitasoft.engine.events.model.SHandlerExecutionException;
       import org.bonitasoft.engine.identity.User;
       import org.bonitasoft.engine.platform.LoginException;
       import org.bonitasoft.engine.search.SearchOptionsBuilder;
       import org.bonitasoft.engine.search.SearchResult;
       import org.bonitasoft.engine.search.descriptor.SearchEntitiesDescriptor;
       import org.bonitasoft.engine.search.descriptor.SearchUserDescriptor;
       import org.bonitasoft.engine.search.identity.SearchUsersWhoCanExecutePendingHumanTaskDeploymentInfo;
       import org.bonitasoft.engine.service.TenantServiceAccessor;
       import org.bonitasoft.engine.service.impl.ServiceAccessorFactory;
       import org.slf4j.Logger;
       import org.slf4j.LoggerFactory;
       import org.springframework.beans.factory.annotation.Autowired;
       import org.springframework.context.annotation.ComponentScan;
    
       import jms.config.JmsConfig;
       import jms.model.EventHandlerMessage;
       import jms.service.JmsSenderService;
    
       @SuppressWarnings("serial")
       @ComponentScan({"lib.events","jms.config"})
       public class EventHandler implements SHandler<SEvent> {
    
        private static Logger LOGGER = LoggerFactory.getLogger(EventHandler.class);
    
        long processInstanceId = 0L;
    
        @Autowired
        private JmsConfig jmsConfig;
    
        public EventHandler(long tenantId) {
          this.tenantId = tenantId;
        }
    
        public long tenantId;
    
        @Override
        public void execute(SEvent event) {
         LOGGER.info("Event Handler Bonita : executing event {}", event.getType());
    
    
    SHumanTaskInstance sHumanTaskInstance = (SHumanTaskInstance) event.getObject();
    LOGGER.warn("Event Handler Bonita : Start for the task n°" + sHumanTaskInstance.getId() + ", " + sHumanTaskInstance.getName() );
    processInstanceId = sHumanTaskInstance.getParentProcessInstanceId();
    
    //getBusiennData(processInstanceId);
    Long humanTaskInstanceId = sHumanTaskInstance.getId();
    TenantServiceAccessor tenantAccessor;
    try {
        tenantAccessor = getTenantServiceAccessor();
    
        final ActivityInstanceService activityInstanceService = tenantAccessor.getActivityInstanceService();
        final SearchEntitiesDescriptor searchEntitiesDescriptor = tenantAccessor.getSearchEntitiesDescriptor();
        final SearchUserDescriptor searchDescriptor = searchEntitiesDescriptor.getSearchUserDescriptor();
        SearchOptionsBuilder searchOptionBuilder = new SearchOptionsBuilder(0,10);
        // Build a searcher that returns a humanTask candidates
        final SearchUsersWhoCanExecutePendingHumanTaskDeploymentInfo searcher = 
                new SearchUsersWhoCanExecutePendingHumanTaskDeploymentInfo(humanTaskInstanceId, activityInstanceService, searchDescriptor,searchOptionBuilder.done());
        try{
            searcher.execute();
            SearchResult<User> users = searcher.getResult();
            List<User> userList = users.getResult();
            for(User user : userList){
                LOGGER.info("Event Handler Bonita: user candidate n°" + user.getId() + "," + user.getUserName());
            }
        } catch (Exception e) {
            LOGGER.error("Event Handler Bonita e : Error in Event Handler " + e);
        }
    
    } catch (SHandlerExecutionException e1) {
        // TODO Auto-generated catch block
        LOGGER.error("Event Handler Bonita e1 : Error in Event Handler " + e1);
    }
    

    }

       @Override
        public boolean isInterested(SEvent event) {
            LOGGER.info("Event Handler Bonita - event {} - asks if we are interested in handling this event instance",event.getType());
         boolean isInterested = false;
         // Get the object associated with the event
         Object eventObject = event.getObject();
         // Check if the event is related to a task
          if (eventObject instanceof SFlowNodeInstance) {
           // Verify that the state of the task is ready. (4)
           SFlowNodeInstance flowNodeInstance = (SFlowNodeInstance) eventObject;
                   isInterested = (flowNodeInstance.getType().equals(SFlowNodeType.USER_TASK) && flowNodeInstance.getStateId()==4);
              processInstanceId = flowNodeInstance.getParentProcessInstanceId();
                 }
    
    EventHandlerMessage eventHandlerMessage = new EventHandlerMessage();
    eventHandlerMessage.setTypeEvent(event.getType());
    eventHandlerMessage.setIdDemande(processInstanceId);
    
    JmsSenderService jmsSenderService = new JmsSenderService();
    jmsSenderService.setServerUrl(jmsConfig.getServer_url());
    jmsSenderService.setUserName(jmsConfig.getUser_name());
    jmsSenderService.setPassword(jmsConfig.getPassword());
    
    jmsSenderService.sendTopicMessage(jmsConfig.getDefault_destination_name(), eventHandlerMessage);
    
    return isInterested;
          }
    
         private TenantServiceAccessor getTenantServiceAccessor() throws SHandlerExecutionException {
            try {
                 ServiceAccessorFactory serviceAccessorFactory = ServiceAccessorFactory.getInstance();
                 return serviceAccessorFactory.createTenantServiceAccessor(tenantId);
                } catch (Exception e) {
                  throw new SHandlerExecutionException(e.getMessage(), null);
                 }
               }
    
           @Override
           public String getIdentifier() {
            return UUID.randomUUID().toString();
           }
         }
    
  7. Three properties file under : src/main/resouces : application-dev.properties, application-int.properties and applciation-liv.properties

The content properties files :
# ----------------------------------------
 jms.server-url=my-server
 jms.user-name=my-user
 jms.user-password=my-password
# ----------------------------------------
 jms.default_destination_name=queue1
 jms.reject_destination_name=queue2
 jms.error_destination_name=queue3

When i run the application springboot on eclipse and i select the profile. OK i can retrieve all properties value

But when generate the jar of application and properties file and deploy them in an external tomcat application. another applcaition tomcat was installed and it will use the HandlerEvent Object to execute API's of this application. it works when i put the properties value in the code

I have deploy the jar ganerated in webapp/otherApplication/WEB-INF/lib and i have put the properties file in the same folder as the jar

The jar can't retrieve values from properties file. I have put the file in different directories

I don't have any error message just the log of properties file value : null

Can you hel me please ?

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source