Suppose the source file is hw.java
Bad choice for a class file name in Java :)
I've googled some time,all the solutions requires me to set the directory in fixed manner.
Not really fixed but "warmly suggested" (Maven relies on conventions - the directory layout being one of them - and it is highly recommended to follow Maven's conventions if you decide to use it, less work, less troubles).
But what I want to do is keep hw.java in current directory (.) (...)
I think you'll need at least a directory below the pom.xml
to store java sources.
Can I have that kind of freedom with maven?
Maven is flexible and defaults are configurable but you should really use the conventions (or another tool than Maven). I suggest to create your project using the archetype for a simple project:
mvn archetype:generate -B -DgroupId=com.stackoverflow \
-DartifactId=Q2841166 \
-Dversion=1.0-SNAPSHOT
Then put your sources under src/main/java
(in the appropriate package) and run mvn install
.
Update: It looks like you don't understand what I'm saying or you just don't want to listen. In that case, feel free to modify the build
element as documented in the POM reference (as I said, defaults ARE NOT MANDATORY):
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
...
</build>
</project>
Now, because changing these defaults is not recommended, because Maven doesn't encourage doing this, because it's a bad idea if you're not dealing with a legacy project, because it's useless work if you don't have to, because this will cause more troubles than benefits, don't expect to find a "tutorial" about this. Let me rephrase it: what you're asking for is a "how to shoot myself in the foot" tutorial and that's just ridiculous.
Last thing: if Maven doesn't work the way you want it to work, don't use Maven, that's the best advice I can give you.