-7

I am currently trying to learn how to create websites using Java EE and Spring. Looking at Spring's REST tutorial, however, the part where it explains how to create a project and link to spring essentially gives magic code to copy/paste, without explaining how it works, or how I would go about finding that code block if the tutorial was not there to give it to me. Their other tutorials that I have looked at are similar in this regard. It has a basic tutorial on how Gradle works, but that is insufficient to understand what is going on in the magic code because significant amounts of new syntax are used in their Rest tutorial.

What I must do in order to only use the tools that come with the JDK (ie. javac) to build a Spring application? Does Spring have some hidden (possibly large) set of jars I can simply add as dependencies and then extract into my final jar, and make it work? If not, and I want to compile a Spring application using only javac and other JDK-default tools how can I do so?

I have tried various Google searches for details on how Spring applications are built, but all I can find consists of more tutorials like the one that I linked that give magic code, where I am given something that works in one context, but no explanation of how it does so or how I can modify it for different contexts.

john01dav
  • 1,842
  • 1
  • 21
  • 40
  • The question shows no research. What have you tried? Have you looked at the build tools like maven or gradle? Have you seen what dependencies are being pulled by Spring boot application? Which version of Spring boot are you using? – Prashant Mar 26 '18 at 09:58
  • @Prashant I've been researching this for most of the past 3 days. I have tried to make sense out of Spring's documentation, but almost all of it contains magic code, and is useless for what I am trying to figure out. I have looked into both of those build tools, and it seems like they are heavily pushing me to just let spring automate away building, despite the fact I want to properly understand. I have not seen the dependencies being pulled in by Spring boot, because I have no idea how to do that. I do not know what version I am using, because it is all pulled in via magic code. – john01dav Mar 26 '18 at 10:02
  • I think this video tutorial gonna help You :) enjoy! Building spring boot application on JavaBrains: https://javabrains.thinkific.com/courses/springboot-quickstart – Tom Mar 26 '18 at 10:19
  • For me its unclear what you want. You dont ask specific questions like "What does buildscript {...} do?", nor do you describe what you mean by 'magic code'. The 2nd part of your post (manually setup with only jdk-tools), scatches a topic which has been already answered: [Why you should use build tools](https://stackoverflow.com/a/20788406/8089107), or just google for jar hell. Btw. spring consists of dozens of libraries, depending on dozens of external libraries, just a hint why you dont want to manage dependencies on your own. Result: Unclear what you ask in part 1, part 2 is optimistic. – sn42 Mar 26 '18 at 10:33
  • @sn42 I do ask a specific question: "As such, I would like to ask what I must do in order to only use the tools that come with the JDK (ie. javac)," although I will edit the question to make it more clear. By magic code I mean where there is a code block and instructions to copy and paste said code block, with no explanation of how the code in that block works, or how I would go about creating it myself if I did not have the tutorial. – john01dav Mar 26 '18 at 10:45
  • @sn42 Regarding spring having dozens of libraries and dependencies, is there somewhere I can just download a zip of all of these and add them to my classpath to make it work, or is there something special that Maven or Gradle is doing that isn't just fancy classpath-jar-adding? Adding a large number, even hundreds, of jars is *much* easier than searching, so far fruitlessly, for a good explanation of either gradle or maven. – john01dav Mar 26 '18 at 10:45
  • @jhon01dav There is: You can download the libraries from maven central (yes, pun intended) for example. But the "fancy-classpath-jar-adding" is a bit more than you expect i guess: Different spring versions require different versions of dependendant libraries, which maven or gradle manage. – sn42 Mar 26 '18 at 10:52
  • @sn42 Is there any reason that Spring doesn't just take all of its dependencies, and all of its own code, and extract them into a single jar file (`unzip `, `zip spring.jar -r`), and then publish that? You are right thought that it does more than I thought though. – john01dav Mar 26 '18 at 10:55
  • Spring is quite modular, different features are published in different libraries and you only include what you need. If you have questions regarding the "magic code", google it or search it on SO. If still unclear, people wouldnt mind to answer a well writted, non-duplicate question about specific parts of gradle buildscripts or maven pom files. – sn42 Mar 26 '18 at 10:59
  • Oh and maybe because build systems are quite of a standard when dealing with non-trivial applications i guess. (Dependency management, testing, deployment, etc) – sn42 Mar 26 '18 at 11:05
  • I am unable to understand what you are trying to achieve, and besides question does not show any R&D that has been done by you. – Harsh Mehta Mar 28 '18 at 11:22

2 Answers2

0

Spring boot uses Maven or Gradle as dependency management tools. I am more familiar with maven so will be using it to answer your questions. But before that, you should have a basic understanding of maven, how as well as why it is used. When you navigate to Spring Initializer website, you are provided a form that could be used to select your dependencies from this starter page. You can, optionally select the build tool, programming language and version of spring boot. After you have selected your dependencies, you can generate a spring boot project. After you have opened this project in your IDE, you can see that the downloaded project has a very minimal maven project type structure with one main class that has been annotated with @SpringBootApplication. According to the documentation:

The @SpringBootApplication annotation is equivalent to using @Configuration,@EnableAutoConfiguration, and @ComponentScan with their default attributes

The main(...) of this class simple calls SpringApplication#run which allows spring boot to bootstrap the application and load ApplicationContext. Now since you already have @ComponentScan, all the packages from the current directory would be scanned for spring components which includes @Controller, @RestController, @Service etc. These components will be called by spring as and when needed. The application that you have downloaded also contains POM file. This file defines the dependencies that your application needs. At the top, you have a <parent> which defines the parent pom file that is used by spring to store the versions of the commonly pulled dependencies by spring boot. For example, here is the refernce to the parent pom for spring boot starter web. The application could be packaged as an executable jar or war. To do this, edit your pom file to have following:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

This will allow you to run your application using the command line as: $java -jar your-spring-app.jar. Note that your jar file is stored in target package after you have built your application using $mvn clean package

Prashant
  • 4,775
  • 3
  • 28
  • 47
  • This does not answer my question. The core of it is "I would like to ask what I must do in order to only use the tools that come with the JDK (ie. javac)." This answer explains how to do it with maven, not the default JDK tools, and includes a code block at the bottom with no explanation of how to go about creating such a code block myself, making it magic code. – john01dav Mar 26 '18 at 10:42
  • I am sorry, maven is the standard build tool. You will other wise have to download the dependencies manually, which will be both tedious as well as error prone. – Prashant Mar 26 '18 at 10:53
  • That is exactly what I want to do. I am finding the lack of good documentation for gradle, and the abundance of magic code to be far more tedious and frustrating than manual jar adding is. – john01dav Mar 26 '18 at 10:53
  • @john01dav i am still waiting for you to explain what magic code you mean. Gradle and maven are standard build tools, i am sure we could help out if we finally get a glipse were you are struggling :) – sn42 Mar 26 '18 at 12:04
0

I want to compile a Spring application using only javac and other JDK-default tools

The short answer is you can but it's not worth the tim. You first need to download all Spring jars. The you download their dependent jars too (inspecting each jar's pom.xml file for transitive dependencies). Then you download the dependencies of those jars and so on until all your leaf jars have no further dependencies. Then you build a who-knows-how long classpath string to pass to javac. And eventually you run your class with java using the same classpath + the classes you got from javac.

The above takes days if not weeks. Compared to a minute or two using a build system, such as maven. The analogy that comes to mind is writing a program in assembler: you can do it theoretically but people with not-so-much time prefer using a higher level language.

Let us know if you're wondering about a particular snippet of "magic code".

Alex
  • 1,313
  • 14
  • 28