You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bu...@apache.org on 2008/09/04 14:22:19 UTC

DO NOT REPLY [Bug 45739] New: Problems with BaseDir when the ant is started as Java library from Java Application

https://issues.apache.org/bugzilla/show_bug.cgi?id=45739

           Summary: Problems with BaseDir when the ant is started as Java
                    library from Java Application
           Product: Ant
           Version: 1.7.1
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: Core
        AssignedTo: notifications@ant.apache.org
        ReportedBy: miro@space-comm.com


I try to start ant script using ant as java library from Java application. The
start is realized as follow:
    File buildXmlFile = new File("C:\a\b\z.xml");
    List<String> command = new ArrayList<String>();
    ...
    command.add(...);
    ...
    command.add("-buildfile");
    command.add(buildXmlFile.getAbsolutePath());
    Main ant = new Main();
    ant.startAnt(command.toArray(new String[command.size()]), null, null);

The content of build xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<project name="SSHAntScriptTest" default="default" basedir=".">
    <description>Build file for SSH Ant Script tests.</description>
    <scp file="test.txt" todir="${username}:${password}@${host}:/home/bc"/>
</project>

The xml build file location is different from the execution directory.
When that execution is initiated from the command prompt like that everything
is OK:
c:\somedir>ant -buildfile c:\anotherdir>buildfile.xml

When I try to start the same with the same parameters as java library the
text.txt file can not be founded. Ant looks for the file in execution directory
"c:\somedir\test.txt" instead in buildfile directory "c:\anotherdir\test.txt".

To fix this at the moment the following changes are made:
1. The user.dir is changed:
   File dir = buildXmlFile.getParentFile();
   System.getProperties().put("user.dir", dir.getAbsolutePath());
2. The method runBuild from the Main class is fixed as follow:
   private void runBuild(ClassLoader coreLoader) throws BuildException {
      ...
      project.init();

      // 2008-Sep-04: Bug Fix from Miro.
      project.getBaseDir();

I try to put that fix before project.init() without result.

If you need I can send you one small project as test case.

The big problem is that I do some changes in System Properties and that the
application is multi-thread and multi-tasking and because System Properties are
global (static) for all instances in the current JVM.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #2 from Stefan Bodewig <bo...@apache.org>  2008-09-04 23:25:03 PST ---
There are two different places where basedirs might get resolved.  One is in
Project#setBaseDir which may be invoked with "." if Project#getBasedir is
called
before the Project instance knows its basename.  This would resolve relative
to the current working directory and not the location of the build file.

The other place is ProjectHelper2#onStartElement which is invoked while the
build
file gets parsed, this will resolve relative to the location of the build file.

My guess is that something in your code invokes project.getBasedir() before the
project's build file has been parsed.  The most common reason for this is a
BuildListener invoking getBaseDir in the buildStarted event which is fired
before the build file gets parsed.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739


Stefan Bodewig <bo...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|blocker                     |normal




--- Comment #1 from Stefan Bodewig <bo...@apache.org>  2008-09-04 05:48:56 PST ---
blocker might a bit high as a priority since it isn't really blocking any usage
Ant has been designed for 8-)


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #15 from Stefan Bodewig <bo...@apache.org>  2008-09-08 03:18:48 PST ---
do you think there is anything else we can do or should I close this as
WORKSFORME?


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #4 from Stefan Bodewig <bo...@apache.org>  2008-09-05 00:11:17 PST ---
I think you misunderstood me.  Your code, the code starting Ant, must be
calling getBasedir before it is safe to do so, right now I don't see anything
wrong in Ant (moving getBasedir to runBuild would break a lot of other things
and simply is wrong).

I don't think Ant explicity reads user.dir anyplace, it just does a 
'new File(".")' which is resolved relative to user.dir on some VM
implementations
on some operating systems (other VMs don't consult that property but the
OS process' knowledge about its current working directory and setting user.dir
won't help).


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #12 from Stefan Bodewig <bo...@apache.org>  2008-09-05 02:18:52 PST ---
would you please try my Test.java instead of a different Java class run from a
JUnit test run form Maven 8-)

The later really leaves too many unknowns around.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #10 from Stefan Bodewig <bo...@apache.org>  2008-09-05 02:06:20 PST ---
(In reply to comment #8)
> > Yes, user.dir may be read in some places but neither is directly related to
> > setting the basedir.
> 
> I am not agree with that again. The true is that when I set this value my
> problem is solved which means that somewhere user.dir is used for setting of
> basedir.

That somewhere most likely is the Java class library when it it asked to
resolve File(".").

(In reply to comment #9)

> The build xml file is not full. Place one file inside like that:

Oh yes, it is.  It shows that basedir is set correctly.

>     <scp file="test.txt" todir="${username}:${password}@${host}:/home/bc"/>

If you really need that, OK.  Same Test.java as before

x.xml now is

<project basedir=".">
  <echo>${user.dir}</echo>
  <echo>${basedir}</echo>
  <scp file="x.xml" todir="stefan:PASS@HOST:/home/stefan"/>
</project>

C:\Temp>dir x.xml
 Volume in Laufwerk C: hat keine Bezeichnung.
 Volumeseriennummer: 1027-F564

 Verzeichnis von C:\Temp

Datei nicht gefunden

C:\Temp>c:\j2sdk1.4.2_18\bin\java.exe -classpath
.;..\OSS\ant\build\lib\ant.jar;
..\OSS\ant\build\lib\ant-launcher.jar;..\OSS\ant\build\lib\ant-jsch.jar;..\OSS\a
nt\lib\optional\jsch-0.1.29.jar Test ..\x.xml
user.dir is C:\Temp
args[0] is ..\x.xml
Buildfile: C:\x.xml
     [echo] C:\Temp
     [echo] C:\
      [scp] Connecting to HOST:22
      [scp] done.

BUILD SUCCESSFUL
Total time: 2 seconds

The "Datei nicht gefunden" as response to "dir" above means "file not found"
- have to live with a German OS, sorry - and shows that scp would have failed
if the file was resolved according to user.dir.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #9 from Miroslav Nachev <mi...@space-comm.com>  2008-09-05 01:44:07 PST ---
(In reply to comment #7)
> x.xml is
> 
> <project basedir=".">
>   <echo>${user.dir}</echo>
>   <echo>${basedir}</echo>
> </project>

The build xml file is not full. Place one file inside like that:
    <scp file="test.txt" todir="${username}:${password}@${host}:/home/bc"/>
When scp try to find the file, the file will miss because ant will try to find
it in user.dir instead basedir.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #5 from Miroslav Nachev <mi...@space-comm.com>  2008-09-05 00:36:43 PST ---
When I am calling project.getBaseDir() before project.init() which is located
in runBuild the problem is not solved because the retrieved directory is the
place from where the application is started instead to be the parent directory
of the build file. I need baseDir to be parent directory of build file.

Also I am not agree that in the code System.getProperty("user.dir") is not
used. The actual usage is:
   System.getProperty("user.dir") - 10 times
      1 time in org.apache.tools.ant.Main
      1 time in org.apache.tools.ant.launch.Locator
      1 time in org.apache.tools.ant.taskdefs.Execute
      1 time in org.apache.tools.ant.taskdefs.optional.ejb.IPlanetEjbc
      1 time in org.apache.tools.ant.types.Path
      1 time in org.apache.tools.ant.util.FileUtils
      1 time in org.apache.tools.ant.ProjectTest
      3 times in org.apache.tools.ant.util.FileUtilsTest
   System.getProperties().put("user.dir", ...) - 2 times
      2 times in org.apache.tools.ant.taskdefs.Execute


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739


Stefan Bodewig <bo...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME




-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #14 from Stefan Bodewig <bo...@apache.org>  2008-09-05 03:25:10 PST ---
so you'd need to find out which part it is that causes the problem, running
within your code, within JUnit or within surefire.

I'd start by wrapping a JUnit test around my Test.java code and running that
within Maven.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #7 from Stefan Bodewig <bo...@apache.org>  2008-09-05 01:18:15 PST ---
Let's see.

Test.java is

import org.apache.tools.ant.Main;

public class Test {
    public static void main(String[] args) throws Exception {
        System.out.println("user.dir is " + System.getProperty("user.dir"));
        System.out.println("args[0] is " + args[0]);
        new Main().startAnt(new String[] {"-f", args[0]}, null, null);
    }
}

x.xml is

<project basedir=".">
  <echo>${user.dir}</echo>
  <echo>${basedir}</echo>
</project>

C:\Temp>c:\j2sdk1.4.2_18\bin\java.exe -classpath
..\OSS\ant\build\lib\ant.jar;..
\OSS\ant\build\lib\ant-launcher.jar;. Test x.xml
user.dir is C:\Temp
args[0] is x.xml
Buildfile: C:\Temp\x.xml
     [echo] C:\Temp
     [echo] C:\Temp

BUILD SUCCESSFUL
Total time: 0 seconds

C:\Temp>copy x.xml ..
        1 Datei(en) kopiert.

C:\Temp>c:\j2sdk1.4.2_18\bin\java.exe -classpath
..\OSS\ant\build\lib\ant.jar;..
\OSS\ant\build\lib\ant-launcher.jar;. Test ..\x.xml
user.dir is C:\Temp
args[0] is ..\x.xml
Buildfile: C:\x.xml
     [echo] C:\Temp
     [echo] C:\

BUILD SUCCESSFUL
Total time: 0 seconds

so basedir is the directory of the build file and not the current working
directory in my case.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #3 from Miroslav Nachev <mi...@space-comm.com>  2008-09-04 23:39:33 PST ---
(In reply to comment #2)
> My guess is that something in your code invokes project.getBasedir() before the
> project's build file has been parsed.  The most common reason for this is a
> BuildListener invoking getBaseDir in the buildStarted event which is fired
> before the build file gets parsed.
> 

If you see my code where I am resolving the problem you will see that I call
project.getBaseDir(). In principle this is not good kind of programming but I
haven't time to re-write the whole Apache ant code.

Unfortunately the second problem with System Properties is still existing. This
can be solved very easy with one helper class SystemUtil which to be instanced
for each ant instance. All calls to/from the System Properties in the existing
ant code to be replaced with that class like systemUtil.getUserDir():
   public class SystemUtil
   {
      private String userDir;

      public String getUserDir()
      {
         if(userDir == null)
         {
            userDir = System.getProperty("user.dir");
         }

         return userDir;
      }

      public void setUserDir(String userDir)
      {
         this.userDir = userDir;
      }
   }
With that code the problem with multi-tasking and multi-threads will be solved.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #13 from Miroslav Nachev <mi...@space-comm.com>  2008-09-05 03:03:16 PST ---
Yes, when the test is not in maven and is not started from JUnit (4.x) the
result is OK.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #6 from Stefan Bodewig <bo...@apache.org>  2008-09-05 01:04:05 PST ---
I'm not suggesting you should invoke project.getBasedir earlier (prior to
project.init), quite the opposite.

Something is calling getBasedir on your project instance before it is safe to
do so, i.e. before the XML file has been parsed which doesn't happen before
ProjectHelper.configureProject in runBuild.

Yes, user.dir may be read in some places but neither is directly related to
setting the basedir.

On a related note, changing the current working directory within a Java process
is almost impossible to do 8and impossible to do in a portable way).  If you
change user.dir it probably doesn't have the desired effect on tasks like
<exec>.  Just take a look at the many silly things the Execute class does
to be able to invoke an executable inside a different working directory than
the one of the current process - simple as long as you can stick with a
specific
JVM on a specific OS.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #8 from Miroslav Nachev <mi...@space-comm.com>  2008-09-05 01:24:43 PST ---
> Yes, user.dir may be read in some places but neither is directly related to
> setting the basedir.

I am not agree with that again. The true is that when I set this value my
problem is solved which means that somewhere user.dir is used for setting of
basedir. Look at method onStartElement of ProjectHelper2.ProjectHandler class:
project.setBaseDir(FILE_UTILS.resolveFile(context.getBuildFileParent(),
baseDir));

It is similar in ProjectHelperImpl, etc.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

DO NOT REPLY [Bug 45739] Problems with BaseDir when the ant is started as Java library from Java Application

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45739





--- Comment #11 from Miroslav Nachev <mi...@space-comm.com>  2008-09-05 02:09:06 PST ---
<?xml version="1.0" encoding="UTF-8"?>
<project name="SSHAntScriptTest" basedir=".">
    <description>Build file for SSH Ant Script tests.</description>
    <echo>${user.dir}</echo>
    <echo>${basedir}</echo>
    <scp file="test.txt" todir="${username}:${password}@${host}:/home/bc"/>
</project>


Main ant = new Main();
ant.startAnt(command.toArray(new String[command.size()]), null, null);


INFO: Connecting to dzver4:22
Buildfile:
D:\Works.NB\Java\BundleCloud\bundlecloudpoc\modules\bundlecloudpoc-SSHUtil\src\test\java\com\prosyst\util\ssh\ant\impl\AntScript.xml
     [echo]
D:\Works.NB\Java\BundleCloud\bundlecloudpoc\modules\bundlecloudpoc-SSHUtil
     [echo]
D:\Works.NB\Java\BundleCloud\bundlecloudpoc\modules\bundlecloudpoc-SSHUtil
      [scp] Connecting to dzver4:22

BUILD FAILED
java.io.FileNotFoundException:
D:\Works.NB\Java\BundleCloud\bundlecloudpoc\modules\bundlecloudpoc-SSHUtil\test.txt
(The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at
org.apache.tools.ant.taskdefs.optional.ssh.ScpToMessage.sendFileToRemote(ScpToMessage.java:232)
        at
org.apache.tools.ant.taskdefs.optional.ssh.ScpToMessage.doSingleTransfer(ScpToMessage.java:160)
        at
org.apache.tools.ant.taskdefs.optional.ssh.ScpToMessage.execute(ScpToMessage.java:144)
        at org.apache.tools.ant.taskdefs.optional.ssh.Scp.upload(Scp.java:304)
        at org.apache.tools.ant.taskdefs.optional.ssh.Scp.execute(Scp.java:203)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:357)
        at
org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:142)
        at
org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:93)
        at org.apache.tools.ant.Main.runBuild(Main.java:743)
        at org.apache.tools.ant.Main.startAnt(Main.java:217)
        at
com.prosyst.util.ssh.ant.impl.AntScriptImpl.build(AntScriptImpl.java:79)
        at
com.prosyst.util.ssh.ant.impl.AntScriptImplTest.testBuild(AntScriptImplTest.java:55)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
        at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
        at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
        at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
        at
org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
        at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
        at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
        at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
        at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
        at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
        at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
        at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
        at
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.