Mostrando entradas con la etiqueta SpringSource. Mostrar todas las entradas
Mostrando entradas con la etiqueta SpringSource. Mostrar todas las entradas

viernes, 10 de diciembre de 2010

Managing dependencies with Maven

Content:

Maven abstract.
Tunning your Maven proyect
Maven standard folders
Managing dependencies with Maven
Adding a nature in Eclipse
Maven profiles inheritance

How does Maven work?

Maven uses repositories for searching any dependency you declare in POM.xml, there are strictly only two types of repositories: local and remote.

The local repository (today's theme) refers to a copy on your own installation that is a cache of the remote downloads, and also contains the temporary build artifacts that you have not yet released. (As said in Maven website).

When working with Eclipse, there are two levels for local repositories. Maven builds one repository over your workspace, therefore, if you have any dependency loaded as project in your workspace, it will be taken previous to any other equally-versioned jar.

The second level for local repositories is your personal space in disk. Maven automatically configures a repository inside your user directory (.m2 for Maven 2), and it is used for synchronizing all your projects, Eclipsoid or not.

In the higher step of this ladder, remote repositories. They could be a server in our own intranet, or one of the mirrors proposed by Maven for downloading its huge library.


Let's do a practice example:

Step one: Creation of secundary project.

Open an instance of SpringSource, configure it for working in the brand-new workspace "workspace_a". It doesn't matter the real path, it only matters that it is new and clean.

Create a "Maven Project",







































We have reached the next window using only the "Windows Brain" (next.. next.. next..), until we are prompted for a "Group Id", "Artifact Id" and "Version".

Group Id: Sort of organizational name or hierarchy. It is a good idea that use your domain for all your packets, it could be at the same time your namespace and a good clue for where to locate more of your business.

Artifact Id: Name for this package.

Version: Initial version. SNAPSHOT tells Maven to look for more recent versions in their configured repositories. 






















If an App.java was created, we wont need anymore for this part of the example.

Try to Run as.. Maven Install.

With this command, you are telling Maven to compile, run tests, and, if succeeded, deploy in local Maven repository to share with other projects.

Now close your SpringSource IDE if you can't afford having two IDEs opened.


Step two: Creation of main project.

Let's do it quickly and painless. As we did in other article, New Project -> Spring Template -> MVC Spring Project (or something similar, I'm not reading it right now :P). I call it "war", why not.














We won't run this new project, because we don't need to. What we only want is being able to compile the code.

Write an import sentence with wildcard like the example, and use one inexistent class like me. God kills kittens for less than this.

Run As -> Maven Install

10/12/10 23:07:14 CET: [ERROR] The import es cannot be resolved
10/12/10 23:07:14 CET: [ERROR] UtilsA cannot be resolved to a type
10/12/10 23:07:14 CET: Build errors for war; org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.2:compile (default) on project a: Compiler errors :
error at import es.anavarro.util.*;
       ^
/home/anavarro/Documents/workspace a/war/src/main/java/a/a/a/HomeController.java:10:0::0 The import es cannot be resolved
error at UtilsA a;
^^^^
/home/anavarro/Documents/workspace a/war/src/main/java/a/a/a/HomeController.java:20:0::0 UtilsA cannot be resolved to a type

Now type this in your pom.xml (inside <dependencies> tag, create it if it doesn't exist yet).


<dependency>
    <groupId>es.anavarro</groupId>
    <artifactId>util</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Of course, adapt it to your step-one project. Rebuild the project, if everything went ok, Maven found the dependency and resolved the "import" error.

10/12/10 23:10:30 CET: Refreshing [/war/pom.xml]
10/12/10 23:10:30 CET: [INFO] snapshot es.anavarro:util:0.0.1-SNAPSHOT: checking for updates from org.springframework.maven.snapshot
10/12/10 23:10:44 CET: Maven Builder: AUTO_BUILD 

The other error is damn logical, that class doesn't exist in util project. Let's create it, one blank "UtilsA" class, in its workspace and project, then Run as -> Maven Install. A new version will be deployed in our local repository, with the same name and version, but (invisible to the user) with a newer timestamp.





















Once your "util" project is done, return to the main project, rebuild it, Maven should download your last version, find your "UtilsA" and be able to compile it all. If it doesn't compile by Eclipse build, try Maven Install, it's the important one, and forces checking and download of dependences.

That is the base of Maven approach, easy to version projects and libraries, easy to collaborate, easy to use.


See you on @anavarro_prof

lunes, 6 de diciembre de 2010

Tunning your Maven project

Content:

Maven abstract.
Tunning your Maven proyect
Maven standard folders
Managing dependencies with Maven
Adding a nature in Eclipse
Maven profiles inheritance

Now let's suppose you want to build a new Maven project in SpringSource,

New Project -> Other -> Maven project.

You already know about the benefits of a Maven project and you want to roll your own like a library, without Spring dependencies.

As soon as you have created your project, you notice that your project doesn't have any resource folder as source folder. They doesn't exist in the folder hierarchy either. But you need those folder for Unit Testing (that is something that will be explained afterwards).


Round one, fight!

As a Eclipse user, the first you think is you may create those folders manually- Create a "resources" folder inside "main", and another "resources" inside "test", then register them as "source folders" in Properties -> Java Build Path -> Source -> Add Folder. By selecting the new folders you may access resource files throw classpath.

It's important to sort the order of folders in Order and Export tab, put your main/resources above your test/resources entry.

Now create three files, "a.xml" in "main/resources", a different copy of "a.xml" in "test/resources" and finally one "b.xml" in "test/resources".

Try it please. Then Run As -> Maven Install and you will get a jar file in your target project directory. Unzip your jar, you should find an "a.xml" with the content of "main/resources" and "b.xml".

What did it happened?

Maven will export every file in "source folders", compiled or not, that's the reason of the undesired existence of "b.xml". We shouldn't export our testing data!
If Maven finds two or more files with the same name, it is chosen by classpath priority, so it is important to tune it well in "Order and Export" tab.

Nevertheless, we don't find the result appropriate, because we don't want to export our testing data.

Take a look to your "Order and Export" tab, you can't uncheck your "Source folders" for exporting (the box at the right of the entry), so, this isn't a good solution.


Round two, fight!

Please delete both entries from "Source folders" in Properties -> Java Build Path.

Now go to "Libraries" tab in the same dialog. "Add class folder" and check your main and test "resource" folders.

Is it the same than before? no. Because now you can drift to your "Order and Export" tab and check your "main/resources" folder for exporting, and not the same for "test/resources" one.

Run As -> Maven Install. Unzip your Jar and see how magically you haven't exported your testing resources. :) Terrific.

DELETE your project, without deleting sources or resources. Only the project from your workspace. Reimport it like a "Existing Maven Project" and... Ops, you have lost all your classpath configuration. Yes, you can reimport like a Simple Project as well, but have now experienced what will happen when any of your workmates download your project from any cvs/svn/other repository.

Take a look to your ".classpath" file, there you have all your configuration. But this file won't be uploaded to the repository, and therefore, no mate will be able to export this library correctly.

You have draft this round too ;)


Round 3, fight!

By losing your second round, you must have noticed that you can't win this battle by using local classpath files. Indeed, you won't never export it to the repository, so the solution must be as exportable as functional.

Take a look to your "Java Build Path" again, 

Let's take a look to your pom.xml file. It's quite clean, isn't it? Let's take a look to the "Super POM.xml" from Maven.

See the build entry:

<build>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <finalName>${artifactId}-${version}</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
  </build>


Quite appealing, I will take it as is and drop it in my "pom.xml". Delete and reimport as Maven project.

That is what you will find:
.classpath independent, the information is in your "pom.xml" file.
Files under "testResource" member of "pom.xml" file won't be exported
Appropiate for sharing throw control version repositories, every mate will build the project correctly.


Epilogue

Now clean every fingerprint from this project, create a new one, create the same folders, the same files inside:

src/main/resources
a.xml

src/test/resources
a.xml (different)
b.xml

Delete the project, reimport it like a Maven one again... yes, Maven did it for you.

Those are the standard folders for a Maven project, and, by being standard, as far as you name your folder so, you won't have to configure they as testResource folders, and their content won't be ever exported.

You are 20% smarter than your mates, you woooon! :)


I will check my horrible English tomorrow. Enjoy this article as much as I enjoyed writing it.

domingo, 5 de diciembre de 2010

Maven

Content:

Maven abstract.
Tunning your Maven proyect
Maven standard folders
Managing dependencies with Maven
Adding a nature in Eclipse
Maven profiles inheritance

Maven is one of those useful tools that I had never heard before my current job.

"Maven's primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with:
  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features"
It sounds like marketing, doesn't it? What does it mean?

- It really makes building easier: Well, I suppose it uses Ant for building, just going to the root folder, type "mvn install" and you got your compilation built. It will take any needed dependence (even Maven dependences not yet downloaded) and will compile the project using the pom.xml configuration file.

- Uniform build system: I suppose so.. The great thing is that you have all your architecture-independent configuration in one file, and it doesn't matter most of the details.

I haven't yet discovered the rest of marketing stuff in the real world, but, what makes me crazy is the dependency management.

Let's suppose I'm working in a project, and my friend J needs my project in his. He can always take my code from our repository, completed or not, it compiles or not, every release.
Certainly, he can ask me if I finished it, but is it necessary?.

Maven offers a way to synchronize throw a repository, something like uploading jars to a code repository (bad practice!). Now I can publish every version, snapshot or nigthly-build in that repository with a version number.

My contract for uploading dependences:
- Code compiles.
- Code passes unitary testing.
- Code passes other optional filters like code quality thresholds.
- New major version for backward code incompatibility.
- New minor version for new features
- Resolved bugs doesn't need additional number (if I'm wrong, I will fix this article).

Maven takes care of compiling, testing and other filters throw "mvn deploy" or "mvn install" command. This way, it makes it difficult to upload bad releases.

Every time J releases his project, Maven checks for a new release (under the same version) of my project, downloads it, compiles his project with my new version and upload his project to the repository. J doesn't even know all this :)


That's the same for Spring dependences, which came pre-configured in every Spring Maven project. You can check your pom.xml when you create a mvc project in SpringSource.

New Project -> Other -> Spring Template Project -> Spring MVC Project

Now read your pom.xml file...

My project needs spring-webmvc library with a variable version:
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

The version I need now is:
<org.springframework.roo-version>1.0.2.RELEASE</org.springframework.roo-version>

Look for my dependencies over here, even snapshots:
<repository>
    <id>org.springframework.maven.snapshot</id>
    <name>Spring Maven Snapshot Repository</name>
    <url>http://maven.springframework.org/snapshot</url>
    <releases><enabled>false</enabled></releases>
    <snapshots><enabled>true</enabled></snapshots>
</repository>

Tune my compiler, use a variable java version:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
            <source>${java-version}</source>
            <target>${java-version}</target>
    </configuration>
</plugin>

Use this version for compiling:
<java-version>1.6</java-version>

Load this library only for testing, but don't export it afterwards:
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>

Think of "groupid" like a sort of namespace, "artifactId" like library name, "version" is what you know you need (making possible linking old libraries without caring of new features of the same library linked by other dependencies). Finally, "scope" tells Maven if we need the library for compiling, runtime, or simply testing. Testing won't be loaded in compile time, and won't be exported when it all finish.

All the configuration in a file, architecture independent, quite clean and structured. That's Maven.

That's quite a lot for today, isn't it?

viernes, 3 de diciembre de 2010

Essential tools: SpringSource

What is SpringSource?

As far as I know, that is the most comfortable open-source tool for developing Spring. It is Eclipse based, so you will be able to complete your favourite IDE with other plugins like Subclipse (SVN), CheckStyle or Covertura.

SpringSource comes with Spring project templates, Maven and JUnit, so, if you want Q, it does their best for you to get it ;)

Its goals:
  • J2EE should be easier to use
  • It is best to program to interfaces, rather than classes. Spring reduces the complexity cost of using interfaces to zero.
  • JavaBeans offer a great way of configuring applications.
  • OO design is more important than any implementation technology, such as J2EE.
  • Checked exceptions are overused in Java. A platform shouldn't force you to catch exceptions you're unlikely to be able to recover from.
  • Testability is essential, and a platform such as Spring should help make your code easier to test.
Yes, they accomplish their goals, so it will be my dev tool for coming reviews, examples and articles.

Download it!
springsource.org