简体   繁体   中英

jhipster Unable to change spring run profile to prod - always starts with dev,swagger - Maven as Service

I am doing

./mvnw -Pprod clean verify

And starts as dev and not has prod profile.

Do I have to change something in my pom file?

        <id>prod</id>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-undertow</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <configuration>
                        <filesets>
                            <fileset>
                                <directory>target/classes/static/</directory>
                            </fileset>
                        </filesets>
                    </configuration>
                </plugin>

To set as a service in a debian server I do:

sudo chmod +x /var/lib/nms-api/n2.jar
sudo chown nmsapi:nmsapi /var/lib/nms-api/n1.jar
sudo ln -s /var/lib/nms-api/n2.jar /etc/init.d/nms2
sudo systemctl enable nms2
sudo service nms2 start

Edit

I am using jhipster framework https://www.jhipster.tech/production/ they say:

Please note that this JAR file uses the profile we selected when building it. As it was built using the prod file in the previous section, it will therefore run with the prod profile.

and the ps -aux says:

/usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -jar /var/lib/nms-api/n1.jar

Is there a way to set java -Dspring.profiles.active=prod in the line above when staring as a service?

Edit 2

I set the Environment Variable

sudo -i
root@nms-cp01-vm01:~# export SPRING_PROFILES_ACTIVE=prod

and

 cat /etc/environment 
$ sudo vi /etc/environment 
$ echo $SPRING_PROFILES_ACTIVE
prod
$ sudo echo $SPRING_PROFILES_ACTIVE
prod

And can run in prod profile if

 /usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -jar /var/lib/nms-api/n4test.jar 

and not in prod profile, but in dev if run as root

sudo /usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -jar /var/lib/nms-api/n4test.jar 

You are confusing maven profiles and spring boot profiles. They are two totally separate concepts. Maven profiles are flags for turning on specific steps during building, whereas a spring profile is a runtime flag to tell spring which set of configuration to apply. For mor information on spring profiles you can see here

Spring profiles can be set through systen properties, environment variables, but also part of your maven build. The first two are much more flexible imo.

System properties

java -Dspring.profiles.active=prod

Environmebt Variables

export SPRING_PROFILES_ACTIVE=prod

As Part of a Maven Build profile

 <profiles>
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
        </properties>
        <!-- The rest of your profile here -->
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        ...
        </build>
    </profile>
  </profiles>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM