Last active
May 16, 2022 15:33
-
-
Save mcgivrer/5e02853bbc43dffa242e768ba989f570 to your computer and use it in GitHub Desktop.
Generate a Java project from scratch (v2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $1 -eq ""]; then | |
#---- Display help message --------------------------------------- | |
echo -e "Command line: $0 | |
Usage: | |
------- | |
$0 -n [project_name] -p [project_version] -a [author_name] -e [author_email] -j [java_version] -k [app_package_name] | |
where: | |
- p [project_name] name of the project directory to be generated (will be used capitalized as application name and main class), | |
- v [project_version] the version for the project to be initialized | |
- a [author_name] name of the author (used as commiter), | |
- e [author_email] email of the author (used as commiter), | |
- j [java_version] the JDK version used to build this app. | |
- k [app_package_name] the package nae where to create the [project_name] main class. | |
--- | |
" | |
else | |
#---- set variables for all files --------------------------------------- | |
while getopts ":p:v:a:e:j:k" opt; do | |
case $opt in | |
p) | |
PROJECT_NAME="$OPTARG" | |
;; | |
a) | |
AUTHOR="$OPTARG" | |
;; | |
e) | |
EMAIL="$OPTARG" | |
;; | |
v) | |
VERSION="$OPTARG" | |
;; | |
j) | |
JVERSION="$OPTARG" | |
;; | |
k) | |
APP_PACKAGE_NAME="$OPTARG" | |
;; | |
\?) | |
echo "Invalid option -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
case $OPTARG in | |
-*) | |
echo "$opt need valid argument" | |
exit 1 | |
;; | |
esac | |
done | |
# set parameters | |
export JAVA_VERSION=${JVERSION} | |
export APP_VERSION=${VERSION} | |
export APP_CLASS_NAME=${PROJECT_NAME} | |
export APP_PACKAGE_NAME="com.${PROJECT_NAME,,}.app" | |
export APP_EXE=${PROJECT_NAME} | |
export APP_NAME=${PROJECT_NAME} | |
APP_NAME=($APP_NAME) | |
APP_NAME=${APP_NAME[@]^} | |
export AUTHOR_NAME=$(git config --global user.name) | |
export AUTHOR_EMAIL=$(git config --global user.email) | |
export LIBS=./lib | |
#---- prepare project file structure --------------------------------------- | |
echo "|" | |
echo "|_ Create project directory structure" | |
mkdir $PROJECT_NAME | |
cd $PROJECT_NAME | |
mkdir -p ./src/main/{java,resources/i18n} | |
mkdir -p ./src/test/{java,resources} | |
mkdir -p $LIBS/test | |
mkdir -p ./scripts/ | |
mkdir -p ./temp/ | |
echo "|" | |
echo "|_ Create Readme" | |
#---- start of README.md file --------------------------------------- | |
cat >README.md <<EOL | |
# README | |
## Context | |
This is the readme file for $APP_NAME ($APP_VERSION) | |
Enjoy ! | |
$AUTHOR_NAME | |
EOL | |
#---- end of README.md file --------------------------------------- | |
echo "|" | |
echo "|_ Create build file" | |
#---- start of build.sh file --------------------------------------- | |
curl https://gist.githubusercontent.com/mcgivrer/a31510019029eba73edf5721a93c3dec/raw/52a11265250f8d519d8b08caaa5f833f92babf6d/build.sh -o ./scripts/build.sh | |
curl https://gist.githubusercontent.com/mcgivrer/a31510019029eba73edf5721a93c3dec/raw/4ab33169131ac0c76018e98237e3d9e45f86d591/stub.sh -o ./lib/stub.sh | |
curl https://gist.githubusercontent.com/mcgivrer/a31510019029eba73edf5721a93c3dec/raw/4ab33169131ac0c76018e98237e3d9e45f86d591/options.txt -o ./lib/options.txt | |
curl https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.8.2/junit-platform-console-standalone-1.8.2.jar -o ./lib/test/junit-platform-console-standalone-1.8.2.jar | |
#---- end of build.sh file --------------------------------------- | |
chmod +x ./scripts/build.sh | |
echo "|" | |
echo "|_ Create gitignore file" | |
#---- sart of .gitignore file --------------------------------------- | |
cat >.gitignore <<EOL | |
/.settings | |
/.vscode | |
/.idea | |
.classpath | |
.project | |
/target | |
**/*.class | |
EOL | |
#---- end of .gitignore file --------------------------------------- | |
echo "|" | |
echo "|_ Create app $APP_NAME main class in package ${APP_PACKAGE_NAME//.//}" | |
mkdir -p src/main/java/${APP_PACKAGE_NAME//.//} | |
#---- start of main java class file --------------------------------------- | |
cat >src/main/java/${APP_PACKAGE_NAME//.//}/${APP_CLASS_NAME}.java <<EOL | |
package $APP_PACKAGE_NAME; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Properties; | |
import java.util.ResourceBundle; | |
/** | |
* Main class ${APP_CLASS_NAME} for project ${APP_NAME} | |
* | |
* @author ${AUTHOR_NAME}<${AUTHOR_EMAIL}> | |
* @since ${APP_VERSION} | |
*/ | |
public class ${APP_CLASS_NAME}{ | |
public ResourceBundle messages = ResourceBundle.getBundle("i18n/messages"); | |
public Properties config = new Properties(); | |
public boolean exit = false; | |
public ${APP_CLASS_NAME}(){ | |
System.out.println(String.format("Initialization application %s (%s)", | |
messages.getString("app.name"), | |
messages.getString("app.version"))); } | |
public void run(String[] args){ | |
init(args); | |
loop(); | |
dispose(); | |
} | |
public void init(String[] args){ | |
List<String> lArgs = Arrays.asList(args); | |
try { | |
config.load(this.getClass().getResourceAsStream("/config.properties")); | |
exit = Boolean.parseBoolean(config.getProperty("app.exit")); | |
} catch (IOException e) { | |
System.out.println(String.format("unable to read configuration file: %s", e.getMessage())); | |
} | |
lArgs.forEach(s -> { | |
System.out.println(String.format("- arg: %s", s)); | |
}); | |
} | |
private void loop(){ | |
while(!exit){ | |
// will loop until exit=true or CTRL+C | |
} | |
} | |
private void dispose(){ | |
System.out.println("End of application ${APP_NAME}"); | |
} | |
public static void main(String[] argc){ | |
${APP_CLASS_NAME} app = new ${APP_CLASS_NAME}(); | |
app.run(argc); | |
} | |
} | |
EOL | |
#---- start of test java class file --------------------------------------- | |
mkdir -p src/test/java/${APP_PACKAGE_NAME//.//} | |
cat >src/test/java/${APP_PACKAGE_NAME//.//}/${APP_CLASS_NAME}Test.java <<EOL | |
package com.myappv3.app; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import static org.junit.jupiter.api.Assertions.*; | |
import ${APP_PACKAGE_NAME}.${APP_CLASS_NAME}; | |
class ${APP_CLASS_NAME}Test { | |
${APP_CLASS_NAME} app; | |
@BeforeEach | |
void setUp() { | |
app = new ${APP_CLASS_NAME}(); | |
} | |
@Test | |
void testRun() { | |
app.run(new String[]{}); | |
assertNotNull(app, "The application has not been initialized"); | |
} | |
@Test | |
void testI18nMessages() { | |
String name = app.messages.getString("app.name"); | |
assertEquals("${APP_CLASS_NAME}", name, "Application app.name has not been defined in i18n properties file"); | |
} | |
@Test | |
void testConfig() { | |
app.init(new String[]{}); | |
boolean exitValue = Boolean.parseBoolean(app.config.getProperty("app.exit")); | |
assertTrue(exitValue, "Application app.exit configuration has not been defined in config.properties file"); | |
} | |
} | |
EOL | |
#---- end of main and test java class file --------------------------------------- | |
echo "|" | |
echo "|_ Project $APP_NAME created." | |
mkdir -p src/main/resources | |
#---- start of config.properties file --------------------------------------- | |
cat >src/main/resources/config.properties <<EOL | |
app.exit=true | |
EOL | |
cat >src/test/resources/config.properties <<EOL | |
app.exit=true | |
EOL | |
#---- end of config.properties file --------------------------------------- | |
mkdir -p src/main/resources/i18n | |
#---- start of default i18n/messages.properties file --------------------------------------- | |
cat >src/main/resources/i18n/messages.properties <<EOL | |
app.name=${APP_NAME} | |
app.version=${VERSION} | |
EOL | |
cat >src/test/resources/i18n/messages.properties <<EOL | |
app.name=${APP_NAME} | |
app.version=${VERSION} | |
EOL | |
#---- end of default i18n/messages.properties file --------------------------------------- | |
#---- create git repository --------------------------------------- | |
echo "|" | |
echo "|_ Initialize git repository and commit files with ${AUTHOR_NAME}<${AUTHOR_EMAIL}>" | |
rm -Rf temp | |
git init | |
git config --local user.name "${AUTHOR_NAME}" | |
git config --local user.email ${AUTHOR_EMAIL} | |
git add . | |
git commit -m "Create project ${APP_NAME}" | |
echo "|" | |
echo "|_ DONE !" | |
echo "---" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment