I am trying to build my code using Maven. now I want to configure like this: my dev machine has Java 1.8 and the target WAS server has 1.7. So can I mention this 2 differnet version in pom.xml? and how?
I have tried to give :
...... but getting compilation error : Source and target should be same.
Having source 1.7 and target 1.8 makes no sense. You're missing out on all the language features of Java 8, so why compile it for Java 8, and not for Java 7?
The other way around probably won't work properly either, because the compiler will need to take Java 8 features and convert them to Java 7 features. For some that's simply not possible (lambdas come to mind).
Just use 1.7 for both the source and the target. It doesn't matter that your development version is 1.8; the source merely means that you will not be able to use any Java 8 language features. Unfortunately it will not prevent you from using Java 8 API calls, so just make sure to not use these. (For this specific scenario, my work PC has not just Java 8 but also 7 and even 6 installed.)
That makes absolutely no sense at all. Even worse, 1.8 is the default target for any source version since 1.5. So you go to all this trouble to disable all the extra features, only to be limited to a Java 8 VM where all those features are still enabled...
The -source option specifies the maximum version of language features supported and the -target option specifies the minimum version of the target JVM that can run the generated class files. Any reasoning around what combinations of these options make sense should be based on this. I'm not sure if the Maven compiler plugin has its own opinions about what combinations make sense though. When I specify these options, they're always the same though so I've never encountered the OP's problem before.
So, if you have
This behavior isn't that surprising, if you think about it. But like I said, if I have 1.4 source, it makes more sense to specify a target of 1.4 as well, you just have to specify that explicitly if you're not compiling with javac 1.4 version.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck