Skip to content

Instantly share code, notes, and snippets.

@saintc0d3r
Last active August 29, 2015 14:05
Show Gist options
  • Save saintc0d3r/48b4a9b3744d0ed807bc to your computer and use it in GitHub Desktop.
Save saintc0d3r/48b4a9b3744d0ed807bc to your computer and use it in GitHub Desktop.
Maven's HOW TOs
I. Create a java project using Maven:
-------------------------------------
1. Run command: mvn archetype:generate.
2. On the next displayed prompt, press [ENTER].
3. At next prompt, enter 1-6 option on the prompt. Enter the highest option number (e.g. 6).
4. Enter 'groupId' on the next prompt (e.g. com.mycompanyname).
5. Enter 'artifactId' on the next prompt (e.g. myapplicationname).
6. Enter 'packageName' on the next prompt (e.g. com.mycompanyname).
7. Enter 'version' on the next prompth (e.g. 1.0.0).
8. Run this command to ensure that your java project has been created successfully:
mvn compile exec:java -Dexec.mainClass=com.mycompanyname.App
II. Add a dependency on a Java project created by Maven:
--------------------------------------------------------
1. Open the pom.xml file using text editor app, in the Java project's root directory.
2. Add a <dependency> tag entry inside <dependencies> tag and save the changes. The complete tags would be shown as follow:
<dependency>
<groupId>dependencypackagename</groupId>
<artifactId>dependencyname</artifactId>
<version>dependencyversion</version>
</dependency>
Example:
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
</dependency>
3. Re-run 'mvn compile' command in the command prompt to download the added dependency.
III. Prevent Maven from producing error compilation when the Java project is targeted for Java 7 or 8
-----------------------------------------------------------------------------------------------------
1. Open the pom.xml file using text editor app, in the Java project's root directory.
2. Add these following xml tags inside <project> tag & save the changes:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
3. Try rebuild your java project by running this command: 'mvn compile'
IV. Add your custom Java library (.JAR) into maven's local repository.
----------------------------------------------------------------------
1. Run this maven's command in your command prompt:
mvn install:install-file -Dfile=the_path_of_your_jar_file -DgroupId=the_package_name_of_your_jar
-DartifactId=name_of_your_jar -Dpackaging=jar -Dversion=version_of_your_jar
Example:
mvn install:install-file -Dfile=out/artifacts/m101j_api_jar/m101j-api.jar -DgroupId=com.xtremecode -DartifactId=m101j-api -Dpackaging=jar -Dversion=1.0-SNAPSHOT
V. Import Java project created by Maven into Intellij IDEA:
-----------------------------------------------------------
1. Close current opened Java project in the Intellij IDEA.
2. On the IDEA welcome dialog, click "Import Project" item under Quick Start section.
3. On the next dialog, select your Java project's pom.xml file then click [Ok].
4. On the next dialog, tick "Import Maven projects automatically" option, Automatically download: Sources & Documentation options as well if you like. Click [Next] button.
5. On the next dialog, click [Next] button.
6. On the next dialog, select the JDK's Home path you want to work with the java project. Click [Next] button.
7. On the next dialog, change the project's name if you like. Click [Finish] button.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment