Different ways of loading a file as an InputStream
There are subtle differences as to how the fileName you are passing is interpreted.
Basically, you have 2 different methods: ClassLoader.getResourceAsStream() and Class.getResourceAsStream(). These two methods will locate the resource differently.
In Class.getResourceAsStream(path), the path is interpreted as a path local to the package of the class you are calling it from.
For example calling, String.getResourceAsStream("myfile.txt") will look for a file in your classpath at the following location: "java/lang/myfile.txt".
If your path starts with a /, then it will be considered an absolute path, and will start searching from the root of the classpath.
So calling String.getResourceAsStream("/myfile.txt") will look at the following location in your class path ./myfile.txt.
ClassLoader.getResourceAsStream(path) will consider all paths to be absolute paths.
So calling String.getClassLoader().getResourceAsString("myfile.txt") and String.getClassLoader().getResourceAsString("/myfile.txt") will both look for a file
in your classpath at the following location: ./myfile.txt.
Every mention of location in this post, could be a location in your filesystem itself, or inside the corresponding jar file, depending on the Class and/or ClassLoader you are loading the resource from.
In case, you are loading the class from an Application Server, so your should use
Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)
instead of this.getClass().getClassLoader().getResourceAsStream(fileName). this.getClass().getResourceAsStream() will also work.
String emailRegEx =
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
last name : [a-zA-z]+([ '-][a-zA-Z]+)*
phone : ^\\+?[0-9\\-]+\\*?$
Issue
JDK 13 , using maven project , maven dependencies does not appear in class path
Solution : add below to .classpath
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
Spcifific test classes
mvn test -Phmof,bamboo-app -Dmaven.surefire.debug -Dtest=com.dao.SectionDaoDBTest#createExtraData_OK
Run test in Debug
mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE"
will use 4 threads
mvn -T 4 install
will use 1 thread per available CPU core
mvn -T 1C install
mvn install -pl $moduleName -am
pl – makes Maven build only specified modules and not the whole project.
-am – makes Maven figure out what modules out target depends on and build them too.
-DdependencyLocationsEnabled=false to your MAVEN_OPTS variables. This addition will mean Maven will retain fewer outgoing connections throughout the build and maybe shave a few seconds off your build as well.
mvn spring-boot:run -Dspring.profiles.active=dev
mvn spring-boot:run -Dspring.profiles.active=dev -Drun.profiles=dev
run specific tests
mvn test -Dspring.profiles.active=dev -Dtest=com.api.v1.controller.InfoControllerTest#testGetInfoValidation4
run with multiple profiles
mvn clean install -Psomedet,local-app,create-db -DskipTests