Table of contents
Continue reading Maven – Spring boot snippetsCategory Archives: Maven
Maven error 501 (HTTPS)
If you installed an old version of Netbeans and trying to use maven, it is possible that you are getting error 501 (see this post).
This can happen because Netbeans does use an embedded maven version but it is not up to date.
To solve the problem install Maven system and configure Netbeans to use the system version instead of the embedded one:
Step 1 – Install maven
sudo apt install maven
Step 2 – Configure netbeans
Netbeans -> Options -> java -> Maven: set up : /usr/share/maven
Multiple projects in intellij
Go to:
- View → Tool Windows → Maven projects
- Click +
- Select the pom files of the projects you want to use
- right click on each main and click run file (it will autocreate the configuration)
References:
https://stackoverflow.com/questions/8774024/intellij-working-on-multiple-projects/47174741#47174741
Maven Jar within Intellij
Intellij is great, but its integration with maven still needs some work.
Here are some steps to create a jar from within Intellij Idea (Community 2017)
Maven – configuration for properties file with JAR
Table of contents
- Configuration requirements
- Create the property file
- Configure POM
- Load the property file from class path
Configuration requirements
The desired requirements for this post are the following:
- Output should be a JAR
- There must exist at least one property file (e.g. config.properties). All property files should not be placed inside the JAR, but should be easily accessible by the user.
- It should be easy to add more properties files without changing pom.xml
To address the requirements above we have to do the following steps:
- We create and place the properties file in src/main/resources
- To copy the properties file in the target directory upon build, we add an include rule to the resources section.
- To create the JAR file, we use the maven-jar-plugin
- We should exclude properties files from the JAR build
- We should define the main class for the JAR so that it is executable
- We should copy the properties files from the resources directory to the target directory
- We should change the code to read the properties file from the classpath
Create the property file
Create the property file and place it in src/main/resources
# Server Configuration
server.port=8080
server.hostname=myserver.com
server.max_connections=100
Configure POM
We need to configure POM to create JAR files and include the property file in the output but exclude it in the JAR:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>copy-resources</id> <phase>install</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <excludes> <exclude>**/*.properties</exclude> </excludes> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.blabla.MyMainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
Load the property file from class path
Lastly, to load the properties, we can use the following code:
Properties prop = new Properties(); prop.load(getClass().getResourceAsStream("/config.properties"));
What about JAR within Intellij?
A maven project in Intellij requires additional steps.
For more information read this post
maven – Google maps dependencies
Sample dependencies for google maps in pom.xml:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.maps</groupId> <artifactId>google-maps-services</artifactId> <version>0.1.20</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.7.0</version> </dependency> </dependencies>
maven – java.lang.NoClassDefFoundError: okio.Okio
Add the following dependency to pom.xml:
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.7.0</version> </dependency>
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
Add the following dependency to pom.xml:
To add an implementation that prints log to std out use:
You can read more here
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.36</version> </dependency>
Reference: