• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Maven Testa as standalone (jar) application

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an application and it also have tests which can run with mvn test command. I wanted the tests to develop as standalone jar file. How can I (Develop) export as jar file all the tests in maven.

Regards,

Swaroop Kunduru.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a separate forum for Maven questions. You'd get more/better answers if you'd asked over there.

Maven is designed with the idea that it will build the deployable product and that the deployable product should not contain anything but production elements in it. In other words, no test classes, test data or stuff like that. The standard way of doing testing with Maven involves a completely separate source tree from the production elements. Instead of src/main, there's src/test, and nothing that's in - or produced from - that source tree will be placed in the final product. The tests are all compiled to a temporary directory.

An individual Maven project can only produce one artifact (jar). So the only way that you could run standalone tests from a jar would be to move the tests from src/test/java to src/main/java. And if you did that, the test code would then end up in production, which not only bulks up the production deployable, it introduces possible security exploits.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:There's a separate forum for Maven questions. You'd get more/better answers if you'd asked over there.


That was my doing. This thread started out in test and I moved it to IDEs instead of Build tools. Fixing so it is in the best forum.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maven Jar plugin allows you to packages test files into a jar. You don't need to move source from src/test to src/main. Look at the Usage page and scroll down to "How to create a jar containing test classes"
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Maven Jar plugin allows you to packages test files into a jar. You don't need to move source from src/test to src/main. Look at the Usage page and scroll down to "How to create a jar containing test classes"



I stand (semi) corrected! The procedures listed will allow you to create a test jar. However, that jat will contain only the test classes and resources and will be separate from the jar containing the actual product. At least as I read it. The "easy" way allows you to keep the test in the src/test tree. The "recommended" way is basically what I'd proposed earlier.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really like the "recommended" way simply because once you move your tests outside of the module you are testing, somebody is sure to forget to execute the test, or, heaven forbid, might even cause the tests to stop compiling. I like to tell my developers "Run mvn clean install before you check in.. No exceptions"
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:I don't really like the "recommended" way simply because once you move your tests outside of the module you are testing, somebody is sure to forget to execute the test, or, heaven forbid, might even cause the tests to stop compiling. I like to tell my developers "Run mvn clean install before you check in.. No exceptions"



I think you can reduce that by making the test jar a test dependency in the project. But definitely, out of sight, out of mind. And maintaining 2 projects instead of 1 is more annoying.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could attach the test JAR to your build. Then both the test JAR and app JAR would be in the repository.

I can think of two ways to do this:

1) Set a finalName for the test JAR within the jar:test-jar plugin properties using something like "${project.build.finalName}_tests" and then use the Build Helper plugin (http://mojo.codehaus.org/build-helper-maven-plugin/) to attach the test JAR to the build.

2) Modify the Jar plugin to add an attach option and a classifier option. Actually, I'm rather surprised that these options don't already exist. I can think of some cases where tests for one project would like to depend on tests for another project, especially of those projects are related. I would then submit this patch back to the community.

If I had to do this, I would opt for option 2.
reply
    Bookmark Topic Watch Topic
  • New Topic