Skip to content

Instantly share code, notes, and snippets.

@jsfan3
Created May 9, 2020 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsfan3/aaa0cf87ea394e6c1e557d1b32961dcc to your computer and use it in GitHub Desktop.
Save jsfan3/aaa0cf87ea394e6c1e557d1b32961dcc to your computer and use it in GitHub Desktop.
Modify, compile and upload the local Netbeans Spring Boot project to the destination testing VPS, installing it as a Linux service
#!/bin/bash
# REMEMBER TO REPLACE THE FOLLOWING VALUES
YOUR_TESTING_SERVER="mydomain.net"
SERVERCODE_DIR="MyServer"
SERVERCODE_DIR_PATH="/home/francesco/NetBeansProjects/"
JAR="MyServer-1.0.0-SNAPSHOT.jar"
SERVERCODE_FULLPATH=$SERVERCODE_DIR_PATH$SERVERCODE_DIR
echo "Server code full path: $SERVERCODE_FULLPATH"
echo "Sending SERVER CODE to TESTING server $YOUR_TESTING_SERVER"
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
cd "$SERVERCODE_DIR_PATH"
if [ ! -f "$SERVERCODE_DIR/pom.xml" ]
then
echo "$SERVERCODE_DIR/pom.xml is not available..."
exit 1
fi
# Copy sources to a different location
rm -fR tempCompile
mkdir tempCompile
rsync -va --exclude=target --exclude=logs ./$SERVERCODE_DIR/ ./tempCompile
cd tempCompile
# Set HTTPS
sed -i -r 's/^server.port[:=] *8080//g' ./src/main/resources/application.properties
echo "
server.port: 443
security.require-ssl=true
server.ssl.key-store:/root/keystore.p12
server.ssl.key-store-password: myPassword
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat
" >> ./src/main/resources/application.properties
# Set path for log
sed -i -- 's#'${SERVERCODE_FULLPATH}'/logs/#/root/logs/#g' ./src/main/resources/application.properties
# Create a fat jar to execute the Spring Boot server
mvn -Dfile.encoding=UTF-8 package spring-boot:repackage
cp ./target/$JAR ../Server.jar
cd ..
echo ""
echo ""
# Sping Boot as service info
# https://www.baeldung.com/spring-boot-app-as-a-service
# Send and install the Jar on the server
scp Server.jar root@${YOUR_TESTING_SERVER}:/root/AppBackend.jar
# The following commented code is necessary only one time
: 'Multiline comment:
ssh root@${YOUR_TESTING_SERVER} echo "[Unit]
Description=A Spring Boot application
After=syslog.target
[Service]
User=root
ExecStart=/root/AppBackend.jar
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/AppBackend.service
ssh root@${YOUR_TESTING_SERVER} systemctl enable AppBackend
'
ssh root@${YOUR_TESTING_SERVER} service AppBackend stop
ssh root@${YOUR_TESTING_SERVER} chmod 777 -R logs
ssh root@${YOUR_TESTING_SERVER} mv logs/server.log logs/server.log.old
ssh root@${YOUR_TESTING_SERVER} chmod +x AppBackend.jar
ssh root@${YOUR_TESTING_SERVER} service AppBackend start
# Cleaning
rm -fR tempCompile
rm -f Server.jar
# End
echo "Finished. Try to open: https://${YOUR_TESTING_SERVER}/testme"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment