Guest User

Untitled

a guest
Aug 14th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. Tutorial: Build a JAR file with Maven in 5 minutes
  2.  
  3. Maven likes to give things coordinates to help locate them. Maven coordinates have three components:
  4. the group identifier - this is usually a DNS name in reverse order
  5. the artifact identifier - this is to identify artifacts within the group
  6. the version - this is to distinguish different versions of the same artifact.
  7. Before we can create a project model, we need to decide what the coordinates of the artifacts produced by the project model will be.
  8. In this case, we are creating a .jar file for local consumption only, so we will use the coordinates
  9. group id: localdomain.localhost.tutorial
  10. artifact id: java-archive
  11. version: 1.0-SNAPSHOT
  12. This gives us the following project model
  13. <project>
  14. <modelVersion>4.0.0</modelVersion>
  15. <groupId>localdomain.localhost.tutorial</groupId>
  16. <artifactId>java-archive</artifactId>
  17. <version>1.0-SNAPSHOT</version>
  18. </project>
  19. We save the project model in a file called pom.xml
  20. Convention over configuration
  21. Maven uses convention over configuration, this means that you only need to tell Maven the things that are different from the defaults.
  22. For example, the default packaging is jar, so conveniently for us here, as we want to build a .jar file, we don't have to tell Maven what packaging to use.
  23. There are similar conventions that Maven uses, for example Maven expects that the Java source code to be compiled and packaged within the .jar file will be found in the directory src/main/java and any additional resources that should be copied into the .jar file will be found in the src/main/resources directory
  24. So we can use these defaults to get building our .jar file straight away, we can create a simple Java file, src/main/java/localdomain/localhost/tutorial/Main.java:
  25. package localdomain.localhost.tutorial;
  26. public class Main {
  27. public static void main(String[] args) {
  28. System.out.println("Hello world");
  29. }
  30. }
  31. And then we can ask Maven to package up the .jar file for us
  32. $ mvn package
  33. [INFO] Scanning for projects...
  34. [INFO]
  35. [INFO] ------------------------------------------------------------------------
  36. [INFO] Building java-archive 1.0-SNAPSHOT
  37. [INFO] ------------------------------------------------------------------------
  38. [INFO]
  39. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-archive ---
  40. [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  41. [INFO] skip non existing resourceDirectory src/main/resources
  42. [INFO]
  43. [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ java-archive ---
  44. [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
  45. [INFO] Compiling 1 source file to target/classes
  46. [INFO]
  47. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-archive ---
  48. [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  49. [INFO] skip non existing resourceDirectory src/test/resources
  50. [INFO]
  51. [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ java-archive ---
  52. [INFO] No sources to compile
  53. [INFO]
  54. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-archive ---
  55. [INFO] No tests to run.
  56. [INFO]
  57. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-archive ---
  58. [INFO] Building jar: target/java-archive-1.0-SNAPSHOT.jar
  59. [INFO] ------------------------------------------------------------------------
  60. [INFO] BUILD SUCCESS
  61. [INFO] ------------------------------------------------------------------------
  62. [INFO] Total time: 1.923s
  63. [INFO] Finished at: Fri Jan 03 16:10:54 GMT 2014
  64. [INFO] Final Memory: 12M/156M
  65. [INFO] ------------------------------------------------------------------------
  66. By convention Maven puts the .jar file in the target directory we can take a look at what is inside:
  67. $ jar tvf target/java-archive-1.0-SNAPSHOT.jar
  68. 0 Fri Jan 03 16:10:56 GMT 2014 META-INF/
  69. 133 Fri Jan 03 16:10:54 GMT 2014 META-INF/MANIFEST.MF
  70. 0 Fri Jan 03 16:10:54 GMT 2014 localdomain/
  71. 0 Fri Jan 03 16:10:54 GMT 2014 localdomain/localhost/
  72. 0 Fri Jan 03 16:10:54 GMT 2014 localdomain/localhost/tutorial/
  73. 577 Fri Jan 03 16:10:54 GMT 2014 localdomain/localhost/tutorial/Main.class
  74. 0 Fri Jan 03 16:10:56 GMT 2014 META-INF/maven/
  75. 0 Fri Jan 03 16:10:56 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/
  76. 0 Fri Jan 03 16:10:56 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/java-archive/
  77. 180 Fri Jan 03 16:10:22 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/java-archive/pom.xml
  78. 134 Fri Jan 03 16:10:54 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/java-archive/pom.properties
  79. And we can run the code from the .jar file
  80. $ java -cp target/java-archive-1.0-SNAPSHOT.jar localdomain.localhost.tutorial.Main
  81. Hello world
  82. Understanding the build process
  83. So what exactly happens when you type mvn package?
Add Comment
Please, Sign In to add comment